MSTest 相当于 NUnit 的参数化测试吗?

发布于 2024-08-23 12:31:01 字数 245 浏览 4 评论 0原文

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)
{
   ...
}

使用 MSTest 完成此类任务的最佳方法是什么?我找不到类似的属性集。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

疏忽 2024-08-30 12:31:01

对于使用 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);
}

更多相关信息 此处

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

杀手六號 2024-08-30 12:31:01

这个 帮忙?

本周我添加了一些单元测试
到由 TFS 管理的项目,
所以我决定使用“核心”单元
测试框架可用
VS2008,不幸的是它没有
支持行测试。但它有一个类似的
称为数据驱动单元测试的功能。
通过这种方法,它有点多
实现“简单”变得复杂
RowTest 场景,但它也允许
实现更复杂的。

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.

蓝颜夕 2024-08-30 12:31:01

我的答案与@oscar-e-fraxedas-tormo 类似。
您可以从内部具有 1 到 100 个测试方法的生成类之一进行子类化,并在派生类中提供所有测试逻辑。
在下面的示例中:

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

Ha_ha_ha_Test 类将包含 42 生成的行(方法)。对于此行中的每一行,将调用自定义方法 GetNextDataRow 以提供所需的测试数据。

更多详细信息:

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

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

岁月苍老的讽刺 2024-08-30 12:31:01

实际上,参数化单元测试(PUT)是单元测试的自然推广。 Microsoft Research 有一个名为 Pex 的项目,它将自动为您的被测类 (CUT) 生成 PUT。 Pex 是一个自动测试输入生成工具。 Pex 工具不会自己准备测试数据,而是会找到 CUT 参数的感兴趣的输入并相应地生成单元测试用例。请查看此处

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.

国产ˉ祖宗 2024-08-30 12:31:01

您可以创建一个基类,其中包含测试方法和参数作为虚拟属性。
当您从此类继承时,您只需使用所需的值覆盖属性。
请看示例代码:

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

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; } }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文