简体   繁体   中英

How can I re-run a nunit test programmatically in 3.2?

In nunit 2.6.4, I used below C# code to re-run a failed test:

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

But after upgrading to nunit 3.2, TestExecutionContext.CurrentContext.CurrentTest returns null. How can I re-run a test in 3.2?

如果您由于暂时性的网络错误等原因而导致测试偶尔失败而试图重新运行测试,则NUnit 3.x会引入Retry属性,该属性将重试给定次数。

I was able to achieve this as follows....

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);

The only way I can think of how this could be done in NUnit 3.x is wrapping the test code which can cause the failure in a loop and put a try catch block inside the loop that just continues in the catch block.

You can pass in a retry count parameter and just count it in the loop and then fail the test once you have reached the max number of retries.

I'm afraid that what you were doing in NUnit 2.6.4 was not a supported way of running tests. It only worked by virtue of using a number of internal classes. I suggest using the published APIs and requesting features where you need them rather than using internal classes and methods that may change arbitrarily in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM