diff --git a/azure-pipelines-retry-sample.yml b/azure-pipelines-retry-sample.yml new file mode 100644 index 0000000000..dc697da481 --- /dev/null +++ b/azure-pipelines-retry-sample.yml @@ -0,0 +1,72 @@ +# Azure Pipeline for testing the RetryExtensionSample +# This pipeline builds and runs the RetryExtensionSample to verify it works correctly + +trigger: + branches: + exclude: + - * + +pr: + branches: + include: + - main + - rel/* + +variables: + - name: _TeamName + value: MSTest + - name: SampleProjectPath + value: samples/public/mstest-runner/RetryExtensionSample + +stages: +- stage: TestRetrySample + displayName: Test Retry Extension Sample + jobs: + - job: Windows + displayName: Windows + pool: + name: NetCore-Public + demands: ImageOverride -equals windows.vs2022preview.amd64.open + steps: + - task: UseDotNet@2 + displayName: 'Install .NET SDK' + inputs: + packageType: sdk + useGlobalJson: true + workingDirectory: $(Build.SourcesDirectory) + + - task: DotNetCoreCLI@2 + displayName: 'Restore sample project' + inputs: + command: restore + projects: '$(SampleProjectPath)/RetryExtensionSample.csproj' + feedsToUse: config + nugetConfigPath: '$(SampleProjectPath)/../../NuGet.config' + + - task: DotNetCoreCLI@2 + displayName: 'Build sample project' + inputs: + command: build + projects: '$(SampleProjectPath)/RetryExtensionSample.csproj' + arguments: '--configuration Release --no-restore' + + - task: PowerShell@2 + displayName: 'Run sample with retry (expect success)' + inputs: + targetType: 'inline' + script: | + cd $(SampleProjectPath) + dotnet run --configuration Release --no-build -- --retry-failed-tests 3 + workingDirectory: $(Build.SourcesDirectory) + failOnStderr: false + + - task: PublishTestResults@2 + displayName: 'Publish Test Results' + inputs: + testResultsFormat: 'VSTest' + testResultsFiles: '**/TestResults/**/*.trx' + searchFolder: '$(Build.SourcesDirectory)/$(SampleProjectPath)' + mergeTestResults: true + failTaskOnFailedTests: false + testRunTitle: 'Retry Extension Sample Tests' + condition: always() diff --git a/samples/public/mstest-runner/RetryExtensionSample/FlakySampleTests.cs b/samples/public/mstest-runner/RetryExtensionSample/FlakySampleTests.cs new file mode 100644 index 0000000000..a3921d0525 --- /dev/null +++ b/samples/public/mstest-runner/RetryExtensionSample/FlakySampleTests.cs @@ -0,0 +1,19 @@ +namespace RetryExtensionSample; + +[TestClass] +public class FlakySampleTests +{ + [TestMethod] + public void FlakyTest_FailsFirstTime_PassesOnRetry() + { + if (Environment.GetCommandLineArgs().Any(arg => arg.Contains("Retries") && !arg.EndsWith("2"))) + { + Assert.Fail("Simulated failure on first execution"); + } + } + + [TestMethod] + public void NormalTest_AlwaysPasses() + { + } +} diff --git a/samples/public/mstest-runner/RetryExtensionSample/README.md b/samples/public/mstest-runner/RetryExtensionSample/README.md new file mode 100644 index 0000000000..5baf907f86 --- /dev/null +++ b/samples/public/mstest-runner/RetryExtensionSample/README.md @@ -0,0 +1,41 @@ +# Microsoft.Testing.Extensions.Retry Sample + +This sample demonstrates how to use the `Microsoft.Testing.Extensions.Retry` extension to automatically retry failed tests. This is useful for handling flaky tests that might fail intermittently due to timing issues, network conditions, or other transient failures. + +## What is Microsoft.Testing.Extensions.Retry? + +The Retry extension is a testing platform-level feature that allows you to automatically retry tests that fail. When enabled, if a test fails, the entire test suite will be re-run up to a specified number of times until all tests pass or the maximum retry count is reached. This extension is compatible with any test framework that supports Microsoft.Testing.Platform, and is not specific to MSTest. + +### Important: Difference from `[Retry]` Attribute + +1. **`Microsoft.Testing.Extensions.Retry`** (this sample): A **platform-level** extension that retries the **entire test suite** when any test fails. Activated via `--retry-failed-tests` command-line option. +2. **`[Retry]` attribute**: A **framework-level** attribute that retries individual test methods. Applied directly to test methods like `[TestMethod]` and `[Retry(3)]`. This is specific to MSTest. + +Use the platform-level retry extension when you want to handle environment-level failures that affect multiple tests. Use the `[Retry]` attribute when specific test methods are known to be flaky. + +## Key Features + +- Automatically retries failed tests +- Configurable retry count +- Failure threshold policies (max percentage, max tests count) +- Works at the test suite level + +## How to Use + +### 1. Add the Package Reference + +Add the `Microsoft.Testing.Extensions.Retry` package to your project: + +```xml + +``` + +### 2. Run Tests with Retry Enabled + +Run your tests with the `--retry-failed-tests` command-line option: + +```bash +dotnet test --project RetryExtensionSample.csproj --retry-failed-tests 3 +``` + +This will retry failed tests up to 3 times. diff --git a/samples/public/mstest-runner/RetryExtensionSample/RetryExtensionSample.csproj b/samples/public/mstest-runner/RetryExtensionSample/RetryExtensionSample.csproj new file mode 100644 index 0000000000..6e4bb8958b --- /dev/null +++ b/samples/public/mstest-runner/RetryExtensionSample/RetryExtensionSample.csproj @@ -0,0 +1,10 @@ + + + + net8.0 + enable + enable + true + + +