Tuesday, April 3, 2018

Parameterized tests with MSTest

I’ve always been an NUnit or XUnit user, but from time to time I take another look at MSTest to see where Microsoft is going with their Test framework.

With the latest version MS-Test 2, Microsoft introduced a new feature: parametrized tests (Do I have to mention that this feature exists for a long time in NUnit and XUnit? Winking smile).

Let’s try it:

  • Create a new Test project in Visual Studio 2017. Th test Project template already includes the MsTest.TestAdapter and MsTest.TestFramework NuGet packages.
  • Right click on your project, select Manage NuGet packages… Update the test packages to the latest version. The default packages are a little bit outdated.
  • Let’s write our parameterized test:
  • Notice that there are some small differences:
    • Instead of the TestMethod attribute, we are using the DataTestMethod attribute
    • Instead of a method without parameters, we expect one parameter as part of our test.
  • Now we have to provide the different possible values for the test using the DataRow attribute:
  • Let’s now run the test:


    No comments: