MSTest Equivalent for NUnit's Parameterized Tests?

試著忘記壹切 提交于 2019-11-27 09:11:07

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.

For those using MSTest2, DataRow + DataTestMethod is 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

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

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.

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

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; } }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!