如何为NUnit的TestCase测试用例配置重试机制?
我太懂这种困扰了——NUnit里给带[TestCase]的测试加重试确实不像普通[Test]那么直接,默认的[Retry]属性直接贴在方法上根本不会生效,因为每个TestCase本质是独立的测试实例,不是方法本身。下面给你几个实操性强的解决方案,按需选就行:
方案一:自定义带重试功能的TestCase属性(最灵活,支持单独设置重试次数)
如果需要给不同的TestCase设置不同的重试次数,写个自定义属性是最好的办法。这个属性继承自NUnit的TestCaseAttribute,内置了重试逻辑:
using System.Reflection; using NUnit.Framework; using NUnit.Framework.Internal; using NUnit.Framework.Internal.Commands; public class RetryTestCaseAttribute : TestCaseAttribute { private readonly int _retryCount; // 重载1:默认重试1次,只传测试参数 public RetryTestCaseAttribute(params object[] arguments) : base(arguments) { _retryCount = 1; } // 重载2:指定重试次数+测试参数 public RetryTestCaseAttribute(int retryCount, params object[] arguments) : base(arguments) { _retryCount = retryCount; } public override TestCommand BuildFrom(MethodInfo method, Test suite) { // 先构建原TestCase的执行命令,再用重试命令包装它 var originalCommand = base.BuildFrom(method, suite); return new RetryCommand(originalCommand, _retryCount); } } // 负责实现重试逻辑的命令类 public class RetryCommand : DelegatingTestCommand { private readonly int _maxRetries; public RetryCommand(TestCommand innerCommand, int maxRetries) : base(innerCommand) { _maxRetries = maxRetries; } public override TestResult Execute(TestExecutionContext context) { int currentRetry = 0; TestResult result; do { result = innerCommand.Execute(context); currentRetry++; } // 失败且未达到最大重试次数时,继续重试 while (result.ResultState.Status == TestStatus.Failed && currentRetry <= _maxRetries); return result; } }
用法也很简单,直接替换原来的[TestCase]就行,还能指定重试次数:
// 示例1:默认重试1次 [RetryTestCase("Chrome", Category = "Test category", TestName = "Name of test", Description = "Description of test")] public void SomeTestName(string browser) { // Test script } // 示例2:指定重试2次 [RetryTestCase(2, "Firefox", Category = "Test category", TestName = "Firefox test", Description = "Firefox specific test")] public void SomeTestName(string browser) { // Test script }
方案二:TestFixture级统一重试(最简单,适合全局统一配置)
如果你的测试类里所有TestCase都需要相同的重试次数,直接在[TestFixture]上贴[Retry]属性就行,不用改每个TestCase:
[TestFixture] [Retry(1)] // 所有测试失败后重试1次 public class MyTestClass { [TestCase("Chrome", Category = "Test category", TestName = "Name of test", Description = "Description of test")] public void SomeTestName(string browser) { // Test script } [TestCase("Edge")] public void AnotherTest(string browser) { // 这个测试也会自动重试1次 } }
方案三:升级NUnit版本后直接用[Retry](最省心,前提是版本允许)
NUnit 3.10及以后的版本,其实已经支持直接给带[TestCase]的方法加[Retry]属性了,可能你之前用的版本太旧?试一下这种写法:
[TestCase("Chrome", Category = "Test category", TestName = "Name of test", Description = "Description of test")] [Retry(1)] public void SomeTestName(string browser) { // Test script }
至于你说的动态重试属性没效果,大概率是因为动态属性没有正确绑定到每个TestCase的实例上——毕竟每个TestCase是独立的测试节点,不是方法本身,普通的动态属性可能只作用于方法级,没覆盖到实例级的测试执行流程。
内容的提问来源于stack exchange,提问作者Agent Shoulder
相关产品推荐
相关产品推荐

