我有一套 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 aTestFixture
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 插件( NuGet 、 GitHub )。使用示例:
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();
}
另请参阅
- Feature - Add 'Retry Attribute' to repeat test upon failure 。关于 Launchpad 上的功能的讨论
关于unit-testing - 多次运行 NUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26207648/