MSTest Equivalent for NUnit's Parameterized Tests?

C#Unit TestingNunitMstestParameterized Unit-Test

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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.

Solution 3 - C#

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

Solution 4 - C#

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

Solution 5 - C#

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.

Solution 6 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionblasterView Question on Stackoverflow
Solution 1 - C#Izzy RodriguezView Answer on Stackoverflow
Solution 2 - C#Jorge FerreiraView Answer on Stackoverflow
Solution 3 - C#Aseem BansalView Answer on Stackoverflow
Solution 4 - C#Dmytro ZhariiView Answer on Stackoverflow
Solution 5 - C#smwikipediaView Answer on Stackoverflow
Solution 6 - C#Oscar FraxedasView Answer on Stackoverflow