xunit测试事实多次 [英] xunit test Fact multiple times

查看:121
本文介绍了xunit测试事实多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些方法需要依靠一些随机计算来提出建议,并且我需要运行几次《事实》以确保确定.

I have some methods that rely on some random calculations to make a suggestion and I need to run the Fact several times to make sure is ok.

我可以在要测试的事实中包含一个for循环,但是由于要在其中执行多个测试,因此我在寻找一种更简洁的方法,例如junit中的Repeat属性:

I could include a for loop inside the fact i want to test but because there are several test where I want to do this I was lookup for a cleaner approach, something like this Repeat attribute in junit: http://www.codeaffine.com/2013/04/10/running-junit-tests-repeatedly-without-loops/

我可以轻松地在xunit中实现类似的东西吗?

Can I easily implement something like this in xunit?

推荐答案

您必须创建一个新的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()
{
    ...
}

这篇关于xunit测试事实多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆