此功能需要 Microsoft.Testing.Extensions.Retry NuGet 包。
小窍门
使用 Microsoft.Testing.Platform.MSBuild (由 MSTest、NUnit 和 xUnit 运行程序以可传递方式包含),安装其 NuGet 包时会自动注册此扩展 ,无需更改代码。 仅当通过设置 <GenerateTestingPlatformEntryPoint>false</GenerateTestingPlatformEntryPoint>禁用自动生成的入口点时,才需要以下部分中的手动注册。
手动注册
var builder = await TestApplication.CreateBuilderAsync(args);
builder.AddRetryProvider();
重试
.NET 测试复原能力和暂时性故障处理扩展。
此扩展用于集成测试,其中测试严重依赖于环境的状态,并且可能会遇到暂时性故障。
注释
该包附带严格的 Microsoft.Testing.Platform 工具许可证。 https://www.nuget.org/packages/Microsoft.Testing.Extensions.Retry/1.0.0/License 提供完整的许可证。
选项
| Option | Description |
|---|---|
--retry-failed-tests |
重新运行任何失败的测试,直到通过或达到最大尝试次数为止。 激活扩展功能所必需的。 |
--retry-failed-tests-max-percentage |
当失败的测试用例百分比超过指定的阈值时,避免重新运行测试。 不能与 --retry-failed-tests-max-tests 结合使用。 |
--retry-failed-tests-max-tests |
当失败的测试用例数量超过指定的限制时,避免重新运行测试。 不能与 --retry-failed-tests-max-percentage 结合使用。 |
还需要同时设置阈值选项(--retry-failed-tests-max-percentage和--retry-failed-tests-max-tests)以及--retry-failed-tests。
例子
可以将失败的测试最多重试 3 次。
dotnet run --project Contoso.MyTests -- --retry-failed-tests 3
重试失败的测试最多 2 次,但如果超过 50% 的测试失败,请停止重试:
dotnet run --project Contoso.MyTests -- --retry-failed-tests 2 --retry-failed-tests-max-percentage 50
重试失败的测试最多 3 次,但如果超过 10 个测试失败,请停止重试:
dotnet run --project Contoso.MyTests -- --retry-failed-tests 3 --retry-failed-tests-max-tests 10
局限性
- 浏览器平台上不受支持。
- 热重载模式中不支持。