如何在3.2中以编程方式重新运行nunit测试?

时间:2016-04-19 22:22:44

标签: c# nunit nunit-3.0

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

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

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

4 个答案:

答案 0 :(得分:0)

如果您尝试重新运行测试,因为它们偶尔因瞬态网络错误等而失败,NUnit 3.x引入了Retry属性,该属性将重试测试一定次数。< / p>

答案 1 :(得分: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);

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

我担心你在NUnit 2.6.4中所做的事情不是支持运行测试的方式。它只能通过使用许多内部类来工作。我建议使用已发布的API并在需要的地方请求功能,而不是使用可能在将来任意改变的内部类和方法。

相关问题