且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

xunit测试事实多次

更新时间:2023-11-30 17:32:40

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

You'll have to create a new DataAttribute to tell xunit to run the same test multiple times.

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

Here's is a sample following the same idea of 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);
    }
}

使用此代码后,您只需要将Fact更改为Theory并像这样使用Repeat:

With this code in place, you just need to change your Fact to Theory and use the Repeat like this:

[Theory]
[Repeat(10)]
public void MyTest()
{
    ...
}