Search code examples
visual-studiounit-testingmstestvs-unit-testing-framework

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?


Solution

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