NUnit 顺序/组合问题

发布于 2024-11-30 09:49:10 字数 261 浏览 1 评论 0原文

我正在编写一些单元测试并想要使用 Sequential 标签,我已经找到了声明此类测试的语法。

[Test, Sequential]
    public void TestCalculations(
    [Values(10000, 15000, 20000, 25000, 50000)] int salary)
    {


    }

在进行诸如具有多个输入的顺序/组合之类的测试时,如何处理断言?

最好的祝愿

I am writing some unit tests and want to make use of Sequential tag, I have found the syntax for delclaring such a test.

[Test, Sequential]
    public void TestCalculations(
    [Values(10000, 15000, 20000, 25000, 50000)] int salary)
    {


    }

How are assertions handled when doing tests like sequential/combinatorial with multiple inputs?

Best Wishes

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

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

发布评论

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

评论(2

同尘 2024-12-07 09:49:10

我自己没有使用过这些属性,但我希望编写实际的测试方法主体,就像为单个值编写它一样。基本上,您一次只会看到一个值,因此只需编写代码来测试该值即可。

鉴于文档,我不认为顺序 对于您的示例确实有意义,因为您只有一个参数。当您有多个参数时,这是有意义的,并且基本上表示一个参数的第一个值应与另一个参数的第一个值配对,然后是每个参数的第二个值,等等,而不是每个参数可能正在执行的对。您可以使用它来提供输入和预期输出,例如:

[Test, Sequential]
public void TestDivisionBy2(
    [Values(10, 25, 40)] int input,
    [Values(5, 12, 20)] int expectedOutput)
{
    Assert.AreEqual(expectedOutput, input / 2);
}

I haven't used these attributes myself, but I'd expect to write the actual test method body exactly as if you were writing it for a single value. Basically you'll only be presented with one value at a time, so just write the code to test that value.

Given the documentation, I don't think Sequential really makes sense for your example, because you've only got one parameter. It makes sense when you've got multiple parameters, and basically says that the first value for one parameter should be paired with the first value for another parameter, then the second value for each etc, rather than each possible pair being executed. You might use that to give input and expected output, for example:

[Test, Sequential]
public void TestDivisionBy2(
    [Values(10, 25, 40)] int input,
    [Values(5, 12, 20)] int expectedOutput)
{
    Assert.AreEqual(expectedOutput, input / 2);
}
温柔女人霸气范 2024-12-07 09:49:10

在某些情况下 TestCase 属性可能很有用,它提供内置功能 Result

[TestCase(12,3, Result=4)]
[TestCase(12,2, Result=6)]
[TestCase(12,4, Result=3)]
public int DivideTest(int n, int d)
{
  return( n / d );
}

如果您不期望任何特定的结果值,而只是想确保测试在不同的输入值下正常工作 - 您也可以这样做:

[TestCase(120000, -1)]
[TestCase(Int32.MinValue, Int32.MaxValue)]
[TestCase(-1, 23333)]
public void ShouldHandleWrongRangeValues(int n, int d)
{
     Assert.DoesNotThrow(() => { var someIntermediateValue = n * d; });
}

In some cases TestCase attribute could be useful, it provides built in feature Result:

[TestCase(12,3, Result=4)]
[TestCase(12,2, Result=6)]
[TestCase(12,4, Result=3)]
public int DivideTest(int n, int d)
{
  return( n / d );
}

If you do not expect any specific result value and just want to ensure that test works fine with different input values - you can do it as well:

[TestCase(120000, -1)]
[TestCase(Int32.MinValue, Int32.MaxValue)]
[TestCase(-1, 23333)]
public void ShouldHandleWrongRangeValues(int n, int d)
{
     Assert.DoesNotThrow(() => { var someIntermediateValue = n * d; });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文