One of the features I miss when working with MSTest is Row Test or TestCase for you NUnit crowd. At times I want to be able to run the same test with different parameters:
[Row(0, 1, 1)]
public void TestRowAdd(int number1, int number2, int result)
Assert.AreEqual(result, number1 + number2);
I know this sample is somewhat trivial but bare with me – I’m trying to make a point…
Of course I could have written the test this way:
public void TestRowAdd()
Assert.AreEqual(3, 1 + 2);
Assert.AreEqual(1, 1 + 0);
Assert.AreEqual(0, -1 + 1);
Assert.AreEqual(-2, -1 + -1);
The problem with this solution is that the first Assert that fails throws the exception and so if the 2nd Assert fails I have no idea what is the end result of the remaining Asserts.
Another solution is to split the test into several tests (one per assert) but it get tedious real quick and then you start to create methods inside your tests that hide the actual implementation:
public void TestRowAdd1()
TestRowAdd(1, 2, 3);
public void TestRowAdd2()
TestRowAdd(1, 1, 0);
The only solution that comes close to real live Row test is to use Data-Driven Test but the downside is that the tests inputs are out of site in an external file.
I’ve been searching for a simple way to add row testing support to MSTest until a good friend pointed out that Microsoft Testing framework has extensibility built in – the good thing is that in VS2010 it just got easier to extend existing functionality.
The the good people of MS Team Test group have provided two tutorials one on how to add the ability to run tests with different privileges and the 2nd tutorial is all about adding RowTest support – and the source code to both tutorials can be found in the MSDN code gallery. Using the code makes the test in the beginning of this post possible and it’s easy to create and customize. The only question I have is why wasn’t RowTest released as part of VS2010 – but until it is part of the “official” MSTest family just create (or copy) your own.
Extending the Visual Studio Unit Test Type, part 1 Extending the Visual Studio Unit Test Type, part 2 Unit Test Extensibility Sample (source code for the tutorials above)