Colonel Panic
Colonel Panic

Reputation: 137584

Visual Studio unit testing - multiple cases like Nunit

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?

Upvotes: 13

Views: 6285

Answers (3)

s.crat
s.crat

Reputation: 66

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);
    }
}

Upvotes: 2

StuartLC
StuartLC

Reputation: 107267

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

Upvotes: 3

Ganesh R.
Ganesh R.

Reputation: 4385

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.

Upvotes: 3

Related Questions