xunit test事实多次

rjl*_*pes 17 c# random xunit

我有一些方法依赖于一些随机的计算提出一个建议,我需要运行情况好几次,以确保正常.

我可以包括用于我要测试,但事实上,因为有几个测试,我想这样做,我是一个更简洁的方法查找,像在JUnit这种重复属性中循环:http://www.codeaffine.com/2013/04/10 /磨合的JUnit测试-反复-无-环/

我可以在xunit中轻松实现这样的功能吗?

Mar*_*ldi 28

你必须创建一个新的DataAttribute来告诉xunit多次运行相同的测试.

以下是一个遵循junit相同概念的示例:

public class RepeatAttribute : DataAttribute
{
    private readonly int _count;

    public RepeatAttribute(int count)
    {
        if (count < 1)
        {
            throw new ArgumentOutOfRangeException(nameof(count), 
                  "Repeat count must be greater than 0.");
        }
        _count = count;
    }

    public override IEnumerable<object[]> GetData(MethodInfo testMethod)
    {
        return Enumerable.Repeat(new object[0], _count);
    }
}
Run Code Online (Sandbox Code Playgroud)

有了这个代码后,你只需要你改变FactTheory,并使用Repeat这样的:

[Theory]
[Repeat(10)]
public void MyTest()
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

  • 看起来不再适用了https://github.com/xunit/xunit/issues/1340 (6认同)
  • 似乎不起作用@user357783你能举个例子吗? (2认同)

huh*_*uha 8

少量迭代的最简单方法:使其成为理论而不是事实。为每次迭代插入一行[InlineData]

using Xunit;

namespace MyUnitTests
{
    public class Class1
    {

        [Theory]
        [InlineData]
        [InlineData]
        public void TestThis()
        {
            // test code here
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用 XUnit 2.4.1 进行测试


Mar*_*oPT 6

具有相同的要求,但是可接受的答案代码没有重复测试,因此我将其调整为:

public sealed class RepeatAttribute : Xunit.Sdk.DataAttribute
{
    private readonly int count;

    public RepeatAttribute(int count)
    {
        if (count < 1)
        {
            throw new System.ArgumentOutOfRangeException(
                paramName: nameof(count),
                message: "Repeat count must be greater than 0."
                );
        }
        this.count = count;
    }

    public override System.Collections.Generic.IEnumerable<object[]> GetData(System.Reflection.MethodInfo testMethod)
    {
        foreach (var iterationNumber in Enumerable.Range(start: 1, count: this.count))
        {
            yield return new object[] { iterationNumber };
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在上一个示例中使用Enumerable.Repeat时,它只会运行测试1次,而xUnit却不重复测试。他们可能在不久前改变了某些东西。通过更改为foreach循环,我们可以重复每个测试,但是我们还提供了“迭代次数”。在测试功能上使用它时,您必须向测试功能中添加一个参数,Theory并按如下所示装饰它:

[Theory(DisplayName = "It should work")]
[Repeat(10)]
public void It_should_work(int iterationNumber)
{
...
}
Run Code Online (Sandbox Code Playgroud)

这适用于xUnit 2.4.0。

我创建了一个NuGet ,以防有人感兴趣。


tou*_*ons 5

我知道这是一个旧线程,但如果您需要重复测试少量次,可以应用以下一个小技巧:

首先,创建一些虚拟数据:

public static IEnumerable<object[]> DummyTestData()
{
   yield return new object[] { 0 };
   yield return new object[] { 1 };
   yield return new object[] { 2 };
   yield return new object[] { 3 };
 }
Run Code Online (Sandbox Code Playgroud)

然后使用虚拟数据来强制测试针对每个向量运行。在这种情况下,相同的测试将被调用 4 次(但虚拟数据实际上并未被使用):

private static int _counter = 0;

[Theory]
[MemberData(nameof(DummyTestData))]
public void SomeTest(int dummyParam)     // dummyParam is unused
{
    _counter+= 1;
    DoSomething();

    Assert.True(...);           
}    
Run Code Online (Sandbox Code Playgroud)

我发现这种方法非常有用,并且比创建新属性更方便。

当然,如果您需要可参数化重复次数,这不是一个好的解决方案(尽管我确信有人可以建议一种使我的解决方案可参数化的方法:-))。