简体   繁体   English

使用xunit生成具有整数范围的多个测试用例

[英]Using xunit to generate multiple test cases with integer range

Is there a way to automatically generate multiple test cases in xUnit? 有没有办法在xUnit中自动生成多个测试用例? Or am I doomed to specify each possible input as an InlineData ? 或者我注定要将每个可能的输入指定为InlineData

In the example below, in NCrunch or the VS test runner it looks like a random number is passed in to the test. 在下面的示例中,在NCrunch或VS测试运行器中,它看起来像一个随机数传入测试。 So everytime you run the test, there is a chance it will pass, although it should fail. 因此,每次运行测试时,它都有可能通过,尽管它会失败。

In NUnit the Range attribute actually generates multiple permutations of the test case as per https://github.com/nunit/docs/wiki/Range-Attribute 在NUnit中, Range属性实际上根据https://github.com/nunit/docs/wiki/Range-Attribute生成测试用例的多个排列。

public bool RangeTest(int input)
{

    if (input > 10)
    {
        return false;
    }

    return true;
}

[Theory]
[AutoMockData]
public void RangeTestCase([Range(0, 11)] int test)
{
    var result = RangeTest(test);      

    Assert.True(result);
}

Here is a good explanaition how to make the test: https://andrewlock.net/creating-parameterised-tests-in-xunit-with-inlinedata-classdata-and-memberdata/ In your case you must create a dedicated data class wich will return the date for the test; 以下是如何进行测试的一个很好的解释: https ://andrewlock.net/creating-parameterised-tests-in-xunit-with-inlinedata-classdata-and-memberdata/在您的情况下,您必须创建一个专用的数据类将返回测试日期;

public class GetTestData : IEnumerable<Int32>
{
 public IEnumerator<Int32> GetEnumerator()
 {
    yield return 1;
    yield return 2;
    yield return 3;
    yield return 4;

   IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
 }
}

and the usage of the class will be: 并且该类的用法将是:

[Theory]
[ClassData(typeof(GetTestData ))]
public bool RangeTest(int input)

But for test is a good idea to test only the edge cases. 但是对于测试来说,仅测试边缘情况是个好主意。 You must avoid the randomness in the tests. 您必须避免测试中的随机性。

AutoFixture doesn't provide such a feature out of the box. AutoFixture不提供开箱即用的功能。 There is a way to do that by creation of a custom DataAttribute , but as it has been highlighted by the answer around you shouldn't do that. 有一种方法可以通过创建自定义DataAttribute来实现这一点,但是由于周围的答案已经突出显示,所以不应该这样做。 It sounds quite oxymoronic, but you should avoid randomness when using AutoFixture. 这听起来非常矛盾,但在使用AutoFixture时应该避免随机性。 You still should check the boundary values using multiple instances of eg InlineAuto[Moq]Data attribute, so it fails/passes predictably and for each time. 您仍然应该使用例如InlineAuto[Moq]Data属性的多个实例来检查边界值,因此它每次都会以可预测的方式失败/通过。

As for the generated values, they should be used only when it does't cause randomness. 至于生成的值,只有在不产生随机性时才应使用它们。 For example, it might be: 例如,它可能是:

  • places where input doesn't matter for the current test; 输入对当前测试无关紧要的地方;
  • scenarios when you do identity check - you verify that same value appears as a result; 进行身份检查时的情况 - 验证结果是否显示相同的值; in this case you are not interested in what exactly the value is. 在这种情况下,您对价值究竟是什么并不感兴趣。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM