MSTest V2–improved Microsoft test framework

By Mirek on (tags: MSTest, test framework, unit testing, categories: tools)

Microsoft has finally catched up with their test framework. Now it can easilly challenge all other testing framework players with its functionality and features. Read more for details…

MSTest v2 is released as a nuget packages and is available here https://www.nuget.org/packages/MSTest.TestFramework/1.2.1. To properly discover and run new tests in Visual Studio you also need the adapter https://www.nuget.org/packages/MSTest.TestAdapter/1.2.1.

For details I recommend this blog post series which covers all the new stuff. I will only mention the main features that are available in the new updated Microsoft test framework.
One of them is the parametrization of the test method. This is basically a feature that makes you testing code DRY and clean. It lets you define one test method with mandatory parameters and use that method to test more than one cases. All you need is to decorate the method with proper attributes and provide a test parameters for each case you want to test. This works very simillar to XUnit framework test parametrization.

 [DataRow(0, 0, 0)]
[DataRow(1, 0, 1)]
[DataRow(-1, -2, -3)]
[DataTestMethod]
public void TestAdd(int x, int y, int expected)
{
Assert.AreEqual(expected, new Foo().Add(x, y));
}

Apart from the inline DataRow attributes you can use the DynamicData to use a test cases provided by a property or method.  Here we have the same inconvinient as in XUnit framework. One is that the returning type of the test data method or property must by IEnumerable<object>. So in the worst case you end up with some ugly casting. Another thing is that the methods or the property has to be static so the test data has to be statically resolvable as well.

Another alternative is to use the DataSource which basicaly allows to fetch the test cases from the external tabase table. Finally you can define a class that implements the ITestDataSource interface and provides a custome collection of test case data.

Another cool feature I found my self preety usefuul is the Assert.That singleton instance. Basically assertions during tests are always done with a set of some static helper methods. So you can do

Assert.AreEqual(…)
Assert.IsTrue(…)

but you cannot implement you own assert methods as a extension to the existing Assert  class, since its static. Now with Assert.That you can do it easily. Just define an extension methods like so

public static class AssertExtensions
{
  public static void IsNullOrEmpty(this Assert assert, string value)
    => Assert.IsTrue(string.IsNullOrEmpty(value));
}

and you can now use it in your test methods

Assert.That.IsNullOrEmpty(myNameVar);

Simple and brilliant, isn’t it?

Another feature that is worth mentioning is the ability to customize the test method execution behavior. Yes, you can basically do almost anything with your test methods. All you need to do is to implement your own TestMethodAttibute.

public class MyTestMethodAttribute : TestMethodAttribute, ITestDataSource
{
public override TestResult[] Execute(ITestMethod testMethod)
{        
/* here I can operate on current test method */
return base.Execute(testMethod);    
} }

Cheers