Dynamic tests with mstest and T4 | (0 rating, 0 votes) |
Mar 5th, 2011 |
If you used mstest and NUnit you might be aware of the fact that the former doesn’t support dynamic, data driven test cases. For example, the following scenario cannot be achieved with the out-of-box mstest: given a dataset, create distinct test cases for each entry in it, using a predefined generic test case.
The best result that can be achieved using mstest is a single testcase that will iterate through the dataset. There is one disadvantage: if the test fails for one entry in the dataset, the whole test case fails.
So, in order to overcome the previously mentioned limitation, I decided to create a text template that will generate the test cases for me. As an example, I will write some tests for an integer multiplication function that has 2 bugs in it:
public int Multiply(int a, int b)
{
//This conditions are simulating the 2 bugs
if (a == 0 && b == 1)
return 100;
if (a == 1 && b == 0)
return -100;
return a * b;
}The classical approach (no dynamic test)
Without using any ‘hacks’, one could write the tests for the Multiply function in the following way:
//Tuple description <value of param a, value of param b, expected result>
private static readonly Tuple<int, int, int>[] TestData = new Tuple<int, int, int>[]{
new Tuple<int, int, int>(0,0,0),
new Tuple<int, int, int>(2,3,6),
new Tuple<int, int, int>(1,0,0), //These will trigger one of the bugs
new Tuple<int, int, int>(-2,-3,6),
new Tuple<int, int, int>(0,1,0) //These will trigger one of the bugs
};
[TestMethod]
public void TestMultiply()
{
foreach (var data in TestData)
{
Assert.AreEqual(data.Item3, Multiply(data.Item1, data.Item2),
"Failed for input ({0}, {1})", data.Item1, data.Item2);
}
}
Running the test will surface only one of the bugs, the one triggered by the input (1,0):

This is not only bad because it doesn’t give a complete overview of the bugs but it also violates the principle of one assertion per test because more than one assertion could be triggered in the test case above.













