75

NUnit supports a feature where you can specify a set of data inputs for a unit test to be run multiple times.

[RowTest]
[Row(1001,1,2,3)]
[Row(1,1001,2,3)]
[Row(1,2,1001,3)]
public void SumTests(int x, int y, int z, int expected)
{
   ...
}

What's the best way to accomplish this same type of thing using MSTest? I can't find a similar set of attributes.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
blaster
  • 8,876
  • 11
  • 48
  • 77
  • 1
    possible duplicate of [How to RowTest with MSTest ?](http://stackoverflow.com/questions/347535/how-to-rowtest-with-mstest) – Gishu Jun 22 '10 at 03:25
  • 1
    Ha - looks like you're reading Osherove too :) – Adam Rackis Jan 06 '11 at 21:52
  • 1
    For anyone coming here from Google (10 years later...), skip the accepted answer and jump straight to: https://stackoverflow.com/a/48347447/398630 – BrainSlugs83 Feb 25 '21 at 23:49

6 Answers6

104

For those using MSTest2, DataRow + DataTestMethod are available to do exactly this:

[DataRow(Enum.Item1, "Name1", 123)]
[DataRow(Enum.Item2, "Name2", 123)]
[DataRow(Enum.Item3, "Name3", 456)]
[DataTestMethod]
public void FooTest(EnumType item, string name, string number)
{
    var response = ExecuteYourCode(item, name, number);

    Assert.AreEqual(item, response.item);
}

More about it here

Izzy Rodriguez
  • 2,097
  • 3
  • 21
  • 32
  • 8
    This is an up to date answer to this question and most applicable one. – yakya Dec 07 '18 at 11:17
  • 1
    @sotn -- right? -- I wish we could vote to override the accepted answer (especially on long dead questions like this). -- Just to correct some of the out of date information out there... – BrainSlugs83 Feb 25 '21 at 23:50
21

Would this help?

This week I was adding some unit tests to a project that is managed by TFS, so I decided to use the "core" unit testing framework available with VS2008, and unfortunately it doesn't support RowTests. But it has a similar feature called Data-Driven Unit Test. With this approach it's a bit more complicate to implement the "simple" RowTest scenario, but it allows also to implement more complicate ones.

Jorge Ferreira
  • 96,051
  • 25
  • 122
  • 132
  • 6
    MSTest has the feature implemented actually. Rodriguez's answer is an up to date one.. – yakya Dec 07 '18 at 11:18
10

You can have this capability by writing a small extension of mstest as shown here.

http://blogs.msdn.com/b/vstsqualitytools/archive/2009/09/04/extending-the-visual-studio-unit-test-type-part-2.aspx

Aseem Bansal
  • 799
  • 4
  • 14
1

My answer is similuar to @oscar-e-fraxedas-tormo one.
You can subclass from one of the generated classes that have from 1 to 100 test methods inside and provide all test logic in the derived class. In the example below:

[TestClass]
public class Ha_ha_ha_Test: MsTestRows.Rows.TestRows_42<string>
{
    public override void TestMethod(string dataRow, int rowNumber)
    {
        Console.WriteLine(dataRow);
        Assert.IsFalse(dataRow.Contains("3"));
    }

    public override string GetNextDataRow(int rowNumber)
    {
        return "data" + rowNumber;
    }
}

The class Ha_ha_ha_Test will contain 42 generated rows (methods). For each of this row, the custom method GetNextDataRow will be called in order to provide required test data.

More details:

https://github.com/dzhariy/mstest-rows

Dmytro Zharii
  • 291
  • 4
  • 14
1

Actually, the Parameterized Unit Test (PUT) is a natural generalization of unit test. And Microsoft Research has a project called Pex that will generate the PUT for your Class Under Test (CUT) automatically. Pex is an auto test input generation tool. Instead of preparing the test data yourself, the Pex tool will find the inputs of interest for the parameters of CUT and generate the unit test cases accordingly. Please check here.

smwikipedia
  • 61,609
  • 92
  • 309
  • 482
-3

You can create a base class with the test method and the parameters as virtual properties. When you inherit from this class you only need to override the properties with the desired values. Please see the sample code:

public class Operation
{
    public static int Add(int x, int y)
    {
        return x + y;
    }
}

[TestClass]
public class AddTests : WorkItemTest
{
    protected virtual int First{get { return 0; }}
    protected virtual int Second{get { return 0; }}

    [TestInitialize]
    public virtual void Init()
    {
        //Init code
    }

    [TestCleanup]
    public virtual void Clean()
    {
        //Clean code
    }

    [TestMethod]
    [Description("x+y = y+x")]
    public virtual void Test_operation_commutativity()
    {
        Assert.AreEqual(Operation.Add(Second, First), Operation.Add(First, Second));
    }
}

[TestClass]
public class AddPositiveTest : AddTests
{
    protected override int First { get { return 1; } }
    protected override int Second { get { return 2; } }
}

[TestClass]
public class AddNegativeTest : AddTests
{
    protected override int First { get { return -1; } }
    protected override int Second { get { return -2; } }
}
Oscar Fraxedas
  • 4,467
  • 3
  • 27
  • 32