Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterized Unit Tests with Visual Studio 2015 Intellitest

One feature I've wanted to see in MSTest for a long time has been Parameterized Unit Tests (PUTs). I was excited to hear that Intellitest would be capable of creating said tests. I've started playing with Intellitest, however, and I'm thinking my definition of PUTs is different than Microsoft's.

When I think "PUT", I think TestCases in NUnit, or Theories in xUnit. People much smarter than me seem to use the same terminology.

Can someone tell me if Intellitest is actually capable of creating a PUT in the same way NUnit or xUnit might, or is this an issue of an overloaded term meaning one thing in Intellitest, and another to most other testing frameworks? Thanks.

like image 620
gerg Avatar asked Jul 30 '15 18:07

gerg


People also ask

How do I enable IntelliTest in Visual Studio?

Open your solution in Visual Studio and then open the class file that has methods you want to test. Right-click on a method and choose Run IntelliTest to generate unit tests for the code in your method.

What are parameterized unit tests?

Parameter- ized unit tests separate two concerns: 1) They specify the external behavior of the involved methods for all test arguments. 2) Test cases can be re-obtained as traditional closed unit tests by instan- tiating the parameterized unit tests.


2 Answers

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 use string, 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.

like image 128
Jeremy Caney Avatar answered Sep 19 '22 14:09

Jeremy Caney


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?.

like image 35
gerg Avatar answered Sep 19 '22 14:09

gerg