简体   繁体   English

NUnit顺序/组合问题

[英]NUnit Sequential/Combinatorial Question

I am writing some unit tests and want to make use of Sequential tag, I have found the syntax for delclaring such a test. 我正在编写一些单元测试并希望使用Sequential标签,我已经找到了用于对这样的测试进行定界的语法。

[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 最好的祝愿

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. 鉴于文档 ,我不认为Sequential真的对你的例子有意义,因为你只有一个参数。 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);
}

In some cases TestCase attribute could be useful, it provides built in feature Result : 在某些情况下, 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 );
}

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

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

相关问题 Nunit组合引发TargetParameterCountException - Nunit Combinatorial throws TargetParameterCountException 是否有类似于NUnit的[Combinatorial]用于MS Visual Studio测试的东西? - Is there something similar to NUnit's [Combinatorial] for MS Visual Studio test? 使用组合属性时,Visual Studio下的NUnit极其慢 - NUnit extremely slow under Visual Studio when using combinatorial attribute 使用具有NUnit的Sequential测试属性的数组 - Using an array with NUnit's Sequential test attribute NUnit,CollectionAssert.AreEquivalent(...,...),C#Question - NUnit, CollectionAssert.AreEquivalent(…,…), C# Question Github Actions run Nunit Selenium 测试问题 - Github Actions run Nunit Selenium tests question 关于顺序攻击/尽早完成顺序的问题 - Question On Sequential Attacks / Finishing Sequence Early 如何使用C#自动化框架依次执行两个服务(使用C#,NUnit,Specflow技术) - How to Execute two services sequential using c# automation framework(Using technologies C#, NUnit, Specflow) 使用NUnit的Sequential属性是否是一种有效的策略,可以对每个测试进行一次检查? - Is using NUnit's Sequential attribute a valid strategy to achieve one check per test? 组合优化,其中必须满足几个标准 - Combinatorial optimization where several criteria must be satisfied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM