12

In Nunit one can reuse a test method for multiple cases.

[TestCase(12,3,4)]
[TestCase(12,2,6)]
[TestCase(12,4,3)]
public void DivideTest(int n, int d, int q)
{
  Assert.AreEqual( q, n / d );
}

How can I do this in Visual Studio's test framework?

StuartLC
  • 104,537
  • 17
  • 209
  • 285
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
  • Possible duplicate of [Does MSTest have an equivalent to NUnit's TestCase?](http://stackoverflow.com/questions/1010685/does-mstest-have-an-equivalent-to-nunits-testcase) – Ian Kemp Feb 03 '17 at 10:29

3 Answers3

3

It is not possible out of the box. But for VS 2010 atleast you can write MSTEST Extensions to provide nearly the same feature. Check this blog. But it is not nearly as good as NUnit's TestCase.

Ganesh R.
  • 4,337
  • 3
  • 30
  • 46
3

Unfortunately, MS Test doesn't support RowTests. However, a workaround can be hacked using the DataSource attribute. There is an example here.

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
1

But of course, one can use the DataRow attribute as shown here:

[TestClass]
public class AdditionTests
{    
    [DataTestMethod]
    [DataRow(1, 1, 2)]
    [DataRow(2, 2, 4)]
    [DataRow(3, 3, 6)]
    public void AddTests(int x, int y, int expected)
    {
      Assert.AreEqual(expected, x + y);
    }
}
s.crat
  • 61
  • 3
  • MS Test V2 has this feature since about 2017. But in 2012, when the question was asked and the answers were given, it was not possible with MS Test V1. – gdir Jun 06 '22 at 09:49