MSTest V2 - First impressions

It’s been a while since I’ve tried a new Unit Testing framework. It seemed that between NUnit, XUnit & MSTest I had enough to choose from. I’ve always tried to be pragmatic when choosing a test framework for a new project and when suggesting one to a new client.

Although all .NET unit testing framework are pretty similar there are some differences between them – and I’m not talking about how they call the “Test” attribute.

It always frustrated me that MSTest didn’t seem to change much since it was introduced back in 2005 while both XUnit & NUnit have improved over time with new ideas that made unit testing easier to adopt.

One of the features I missed the most was Parameterized unit tests – the ability to write a test once and run it several times with the same input. Since MSTest is widely used it was frustrating to see good developers write bad tests just because that featured was missing.

Over the years I grow tired of waiting for that support and I’ve tried implementing it myself – with some success but without a lot of problems:

And then those solutions stopped working and I’ve stopped using them.

That’s why I was happy to find out that not only is Microsoft working on a new “MSTest V2” but it will have the parameterized tests I’ve always wanted.

On top of that getting started with it is simple. You no longer have the create a special “Unit Testing” project – any class library will do.

CreateClassLib

And Then just add MSTest.TestFramework & MSTest.TestAdapter using NuGet and you’re ready to go. (don’t forget to mark Include Prerelease)

nugettestadpater

nugetTestadapter

And writing tests just works just like before – even better now we have the ability to write tests with RowTests:

[TestClass]
public class TestFrameworkTest
{
    [TestMethod]
    public void SimpleTest()
    {
        Assert.IsTrue(false);
    }

    [DataTestMethod]
    [DataRow(1, 2, 3)]
    [DataRow(2, 2, 4)]
    [DataRow(3, 2, 6)]
    [DataRow(5, 2, 7)]
    public void RowTest(int a, int b, int result)
    {
        Assert.AreEqual(result, a + b);
    }
}

And it works! I run the tests and from each row runs as a separate test.

image

At the moment there is no way to run only one row – you have to run them all. This means that I cannot debug a single row – which can become very painful with multiple rows. Although NUnit (and XUnit) still have better row testing functionality at least now I can write (and teach how to write) proper tests even when a client has chosen to use MSTest (due to other good reasons).

Seems that MSTest is changing and I can’t wait to see what other new features will come next.

 

Until then – Happy coding…

Labels: , ,