Visual studio parameterized unit test as java

In a Java test environment, I can use parameterized unit tests, as shown in the following code:

@RunWith(value = Parameterized.class) public class JunitTest6 { private int number; public JunitTest6(int number) { this.number = number; } @Parameters public static Collection<Object[]> data() { Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } }; return Arrays.asList(data); } @Test public void pushTest() { System.out.println("Parameterized Number is : " + number); } } 

How can I do this in a Visual Studio unit test project? I cannot find any parameterized attribute or pattern like this.

+6
source share
3 answers

Using the NUnit framework , you must pass parameters for the test as follows:

 [TestCase(1, 2, 3)] [TestCase(10, 20, 30)] public void My_test_method(int first, int second, int third) { // Perform the test } 

This will run two separate times, passing the values 1, 2, 3 in the first run and 10, 20, 30 in the second.

Edit: review of available test videos for NUnit, see this SO question

+8
source

If everything is ok with the NUnit link, check the parameterized tests page. Support for embedded static and dynamic data values.

If for some reason you do not want to use NUnit, unit testing MSTest or VS supports receiving input from CSV, XML or DB. Native support is available through the extension . There is no dynamic support yet .. you need to add dynamic code to your testing method if you want to dynamically calculate inputs / outputs.

+1
source

Now it is also possible with MSTest 2 .

It comes with a DataTestMethod attribute and associated DataRow attributes. What makes it similar in the way NUnit works.

Here are some good examples of how to use it.

0
source

All Articles