简体   繁体   English

MSTest 等效于 NUnit 的参数化测试?

[英]MSTest Equivalent for NUnit's Parameterized Tests?

NUnit supports a feature where you can specify a set of data inputs for a unit test to be run multiple times. NUnit 支持一项功能,您可以在其中指定一组数据输入,以便多次运行单元测试。

[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?使用 MSTest 完成相同类型的事情的最佳方法是什么? I can't find a similar set of attributes.我找不到类似的属性集。

For those using MSTest2, DataRow + DataTestMethod are available to do exactly this:对于使用 MSTest2 的用户,DataRow + DataTestMethod 可用于执行此操作:

[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更多关于这里

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.本周我向一个由 TFS 管理的项目添加了一些单元测试,所以我决定使用 VS2008 提供的“核心”单元测试框架,不幸的是它不支持 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.使用这种方法,实现“简单”的 RowTest 场景有点复杂,但它也允许实现更复杂的场景。

You can have this capability by writing a small extension of mstest as shown here.您可以通过编写 mstest 的一个小扩展来获得此功能,如下所示。

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

My answer is similuar to @oscar-e-fraxedas-tormo one.我的回答与@oscar-e-fraxedas-tormo 相似。
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.您可以从其中具有 1 到 100 个测试方法的生成类之一进行子类化,并在派生类中提供所有测试逻辑。 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). Ha_ha_ha_Test类将包含42生成的行(方法)。 For each of this row, the custom method GetNextDataRow will be called in order to provide required test data.对于每一行,将调用自定义方法GetNextDataRow以提供所需的测试数据。

More details:更多细节:

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

Actually, the Parameterized Unit Test (PUT) is a natural generalization of unit test.实际上,参数化单元测试 (PUT) 是单元测试的自然概括。 And Microsoft Research has a project called Pex that will generate the PUT for your Class Under Test (CUT) automatically. Microsoft Research 有一个名为 Pex 的项目,它会自动为您的待测类 (CUT) 生成 PUT。 Pex is an auto test input generation tool . Pex 是一个自动测试输入生成工具 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. Pex 工具不会自己准备测试数据,而是会找到对 CUT 参数感兴趣输入,并相应地生成单元测试用例。 Please check here .请检查这里

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MSTest 是否与 NUnit 的 TestCase 等效? - Does MSTest have an equivalent to NUnit's TestCase? C#-nunit / mstest等效于junit的@RunWith(HierarchicalContextRunner.class) - C# - nunit/mstest equivalent to junit's @RunWith(HierarchicalContextRunner.class) NUnit参数化测试:设置测试描述 - NUnit Parameterized tests: Setting the test description 单元测试客户的安装(使用NUnit和MSTest) - Unit Testing a Customer's Installation (with NUnit and MSTest) NUnit 相当于 JUnit 的规则 - NUnit equivalent of JUnit's Rule nunit-console不运行使用TestCase属性参数化的测试 - nunit-console does not run tests parameterized with TestCase attribute MSTest中的ClassCleanup是静态的,但是构建服务器使用nunit来运行单元测试。 我该如何调整? - ClassCleanup in MSTest is static, but the build server uses nunit to run the unit tests. How can i adjust? 仅安装 Specflow.MSTest 包时,Specflow 3.0 会错误地生成 NUnit 测试 - Specflow 3.0 incorrectly generating NUnit tests when only Specflow.MSTest package is installed 将 MSTest 转换为 NUnit:NUnit 中的 CurrentTestOutcome 和 UnitTestOutcome 替代方案 - Converting MSTest to NUnit: CurrentTestOutcome and UnitTestOutcome alternatives in NUnit 将参数化的TestFixture移动到基类后,NUnit测试结果不确定。 为什么? - NUnit tests inconclusive after moving parameterized TestFixture to base class. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM