- Approved in principle
- Under discussion
- Implementation
- Shipped
This details the MSTest V2 framework extensibility for specifying custom data source for data driven tests.
Often times, custom data sources are required for data driven tests. User should be able to leverage test framework extensibility to provide custom data sources for test execution.
- A custom data source can be used by multiple test cases.
- A test case can have multiple data sources.
Here is a solution for using custom data source in data driven tests.
The test framework should define an interface class ITestDataSource
which can be extended to get data from custom data source.
public interface ITestDataSource
{
/// <summary>
/// Gets the test data from custom data source.
/// </summary>
IEnumerable<object[]> GetData(MethodInfo methodInfo);
/// <summary>
/// Display name to be displayed for test corresponding to data row.
/// </summary>
string GetDisplayName(MethodInfo methodInfo, object[] data);
}
Here is how the test methods are decorated with concrete implementation of ITestDataSource
:
public class CustomTestDataSourceAttribute : Attribute, ITestDataSource
{
public IEnumerable<object[]> GetData(MethodInfo methodInfo)
{
return new[]
{
new object[] {1, 2, 3},
new object[] {4, 5, 6}
};
}
public string GetDisplayName(MethodInfo methodInfo, object[] data)
{
if (data != null)
{
return string.Format(CultureInfo.CurrentCulture, "{0} ({1})", methodInfo.Name, string.Join(",", data));
}
return null;
}
}
[TestMethod]
[CustomTestDataSource]
public void TestMethod1(int a, int b, int c)
{
Assert.AreEqual(1, a % 3);
Assert.AreEqual(2, b % 3);
Assert.AreEqual(0, c % 3);
}
In a similar way, multiple test methods can be decorated with same data source. A test method can also be decorated with multiple data sources.
Users can customize the display name of tests in test results by overriding GetDisplayName()
method.
public override string GetDisplayName(MethodInfo methodInfo, object[] data)
{
return string.Format(CultureInfo.CurrentCulture, "MyFavMSTestV2Test ({0})", string.Join(",", data));
}
The display name of tests in the above example would appear as :
MyFavMSTestV2Test (1,2,3)
MyFavMSTestV2Test (4,5,6)
The MSTest v2 framework, on discovering a TestMethod
, probes additional attributes. On finding attributes inheriting from ITestDataSource
, the framework invokes GetData()
to fetch test data and iteratively invokes the test method with the test data as arguments.
- Users can extend
ITestDataSource
to support custom data sources. - Multiple tests can reuse the test data defined in the same data source.
- A test case can use multiple test data sources.
When implementing a custom ITestDataSource
(attribute), the GetData()
method should not return an empty sequence, otherwise the test(s) using this data source attribute will always fail.
None.