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? ).
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[DataTestMethod] | |
public void Resize_Should_Succeed_For_SupportedImageVersions(string version) | |
{ | |
ResizeFile(version); | |
} |
- 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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[DataRow("original")] | |
[DataRow("thumbnail")] | |
[DataRow("small")] | |
[DataRow("medium")] | |
[DataRow("large")] | |
[DataRow("square")] | |
[DataTestMethod] | |
public void Resize_Should_Succeed_For_SupportedImageVersions(string version) | |
{ |
- Let’s now run the test: