unit-testing - 多次运行 NUnit 测试

标签 unit-testing nunit resharper ncrunch

我有一套 NUnit 测试,其中一些测试间歇性失败,可能是因为计时问题。我想找到这些不稳定的单元测试。有没有办法多次重复每个测试,而不必在每个测试上放置 Repeat() 属性?我们通常使用 resharper 和 ncrunch 运行程序,但也可以访问 nunit gui 和控制台运行程序。

最佳答案

N单元3

在 NUnit 3 中,您可以使用 Retry属性:

RetryAttribute is used on a test method to specify that it should be rerun if it fails, up to a maximum number of times.

Notes:

It is not currently possible to use RetryAttribute on a TestFixture or any other type of test suite. Only single tests may be repeated.

If a test has an unexpected exception, an error result is returned and it is not retried. Only assertion failures can trigger a retry. To convert an unexpected exception into an assertion failure, see the ThrowsConstraint.

N单元2

NUnit 2 不支持重试,但您可以使用 NUnit-retry 插件( NuGetGitHub )。使用示例:

private static int run = 0;

...

[Test]
[Retry(Times = 3, RequiredPassCount = 2)]
public void One_Failure_On_Three_Should_Pass()
{
    run++;

    if (run == 1)
    {
        Assert.Fail();
    }

    Assert.Pass();
}

另请参阅

关于unit-testing - 多次运行 NUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26207648/

相关文章:

nunit - NUnit 中的 TestActionAttribute 可以在设备自己的 SetUp 方法之前运行 BeforeTest 吗?

c# - 为两个变更集之间的新/修改代码获取 resharper 问题

python - 在单元测试中测试污染

c# - 如何对 IRepository 进行单元测试?

javascript - 如何从 karma 测试中模拟dom元素

c# - 单元测试,确保良好的覆盖率,同时避免不必要的测试

c# - 如何在格式字符串中设置占位符的颜色

c# - 等待流的人口

java - 如何在 TestNG 上的 @BeforeMethod 中添加分组和依赖关系

python - python 中的严格模拟