0

在 nunit 2.6.4 中,我使用以下 C# 代码重新运行失败的测试:

TestExecutionContext.CurrentContext.CurrentTest.Run(new NullListener(), TestFilter.Empty);

但是升级到 nunit 3.2 后,TestExecutionContext.CurrentContext.CurrentTest 返回 null。如何在 3.2 中重新运行测试?

4

4 回答 4

2

如果您尝试重新运行测试,因为它们偶尔会因为暂时的网络错误等而失败,NUnit 3.x 引入了一个Retry属性,它将重试测试给定的次数。

于 2016-04-19T23:26:41.253 回答
0

我能想到如何在 NUnit 3.x 中完成此操作的唯一方法是将可能导致失败的测试代码包装在循环中,并将 try catch 块放入循环中,该循环在 catch 块中继续。

您可以传入重试计数参数并在循环中对其进行计数,然后在达到最大重试次数后使测试失败。

于 2016-04-20T03:06:52.703 回答
0

恐怕您在 NUnit 2.6.4 中所做的并不是运行测试的受支持方式。它只能通过使用一些内部类来工作。我建议使用已发布的 API 并在需要的地方请求功能,而不是使用将来可能任意更改的内部类和方法。

于 2016-04-20T05:07:45.040 回答
0

我能够做到这一点如下......

using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
using NUnit.Framework.Internal.Execution;

TestActionCommand command = new TestActionCommand(CommandBuilder.MakeTestCommand(TestExecutionContext.CurrentContext.CurrentTest as TestMethod));
command.Execute(TestExecutionContext.CurrentContext);
于 2016-04-20T00:53:33.423 回答