此功能需要 Microsoft.Testing.Extensions.Retry NuGet 套件。
小提示
當使用 Microsoft.Testing.Platform.MSBuild (MSTest、NUnit 和 xUnit 執行程式可傳遞包含)時,安裝其 NuGet 套件時,此擴充功能會自動註冊——無需修改程式碼。 以下部分的手動註冊僅在你透過設定 <GenerateTestingPlatformEntryPoint>false</GenerateTestingPlatformEntryPoint>關閉自動產生的入口點時才需要。
手動註冊
var builder = await TestApplication.CreateBuilderAsync(args);
builder.AddRetryProvider();
Retry
.NET 測試復原和暫時性錯誤處理延伸模組。
此延伸模組適用於整合測試,其中測試嚴重相依於環境的狀態,而且可能會遇到暫時性錯誤。
備註
此套件附帶 Microsoft.Testing.Platform Tools 的限制性授權。 完整授權可在 https://www.nuget.org/packages/Microsoft.Testing.Extensions.Retry/1.0.0/License取得。
選項
| 選項 | 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 設定。
範例
重試測試失敗次數最多三次:
dotnet run --project Contoso.MyTests -- --retry-failed-tests 3
重試最多失敗兩次,但如果超過50% 測試失敗,請停止重試:
dotnet run --project Contoso.MyTests -- --retry-failed-tests 2 --retry-failed-tests-max-percentage 50
重試測試失敗最多三次,但如果超過10次失敗,請停止重試:
dotnet run --project Contoso.MyTests -- --retry-failed-tests 3 --retry-failed-tests-max-tests 10
局限性
- 瀏覽器平台不支援。
- 熱換彈模式不支援。