开发者

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.

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


    }

How are assertion开发者_高级运维s 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. 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(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; });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜