Parameterized Unit Tests with Visual Studio 2015 Intellitest
As of June 2016, this feature has been added to "MSTest V2", which can be installed via NuGet by adding the MSTest.TestAdapter
and MSTest.TestFramework
packages:
Install-Package MSTest.TestAdapter
Install-Package MSTest.TestFramework
Be aware that these are different than the version of the test framework that ships with e.g. Visual Studio 2017. To use them, you'll likely need to remove the reference(s) to
Microsoft.VisualStudio.QualityTools.UnitTestFramework
.
Once these are installed, you can simply use the RowDataAttribute
, as demonstrated in the following example:
[TestMethod]
[DataRow(1, 1, 2)]
[DataRow(3, 3, 6)]
[DataRow(9, -4, 5)]
public void AdditionTest(int first, int second, int expected) {
var sum = first+second;
Assert.AreEqual<int>(expected, sum);
}
Obviously, you aren't restricted to
int
here. You can also usestring
,float
,bool
, or any other primitive value type.
This is identical to the implementation previously available to Windows Store App projects, if you're familiar with that.
A Parameterized Unit Test generated by Intellitest is not the same as a PUT typically found in other testing frameworks.
In the MSTest/Intellitest world, PUTs are used to intelligently generate other unit tests.
In order to execute a test multiple times with different sets of data in MSTest, we still need to wrestle with Data-Driven Unit Tests or use MSTestHacks as suggested in How to RowTest with MSTest?.