|
1 | | -// Copyright (c) Microsoft Corporation. All rights reserved. |
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
3 | 3 |
|
4 | 4 | using Microsoft.Testing.Platform.Acceptance.IntegrationTests; |
@@ -167,6 +167,166 @@ public static void ClassCleanup() |
167 | 167 | public TestContext TestContext { get; set; } |
168 | 168 | } |
169 | 169 |
|
| 170 | +[TestClass] |
| 171 | +public sealed class CustomRetryTests : AcceptanceTestBase<CustomRetryTests.TestAssetFixture> |
| 172 | +{ |
| 173 | + [TestMethod] |
| 174 | + public async Task CustomRetryBaseAttribute_ControlsRetriesAndReportsEveryAttempt() |
| 175 | + { |
| 176 | + var testHost = TestHost.LocateFrom(AssetFixture.ProjectPath, TestAssetFixture.ProjectName, TargetFrameworks.NetCurrent); |
| 177 | + string markerDirectory = Path.Combine(Path.GetTempPath(), "mstest-custom-retry-" + Guid.NewGuid().ToString("N")); |
| 178 | + Directory.CreateDirectory(markerDirectory); |
| 179 | + |
| 180 | + try |
| 181 | + { |
| 182 | + TestHostResult testHostResult = await testHost.ExecuteAsync( |
| 183 | + environmentVariables: new() { [TestAssetFixture.MarkerDirectoryEnvVar] = markerDirectory }, |
| 184 | + cancellationToken: TestContext.CancellationToken); |
| 185 | + |
| 186 | + testHostResult.AssertExitCodeIs(ExitCode.Success); |
| 187 | + testHostResult.AssertOutputContainsSummary(failed: 0, passed: 1, skipped: 0); |
| 188 | + testHostResult.AssertOutputContains("failed (try 1) FailsTwiceThenPasses"); |
| 189 | + testHostResult.AssertOutputContains("failed (try 2) FailsTwiceThenPasses"); |
| 190 | + testHostResult.AssertOutputContains("retried:"); |
| 191 | + testHostResult.AssertOutputContains("flaky: 1"); |
| 192 | + |
| 193 | + string markerFile = Path.Combine(markerDirectory, TestAssetFixture.MarkerFileName); |
| 194 | + Assert.IsTrue( |
| 195 | + File.Exists(markerFile), |
| 196 | + $"Custom retry marker file not found. StandardOutput:\n{testHostResult.StandardOutput}"); |
| 197 | + |
| 198 | + string[] observations = File.ReadAllLines(markerFile); |
| 199 | + Assert.Contains("TestExecutions=3", observations); |
| 200 | + Assert.Contains("ExecuteAsyncCalls=1", observations); |
| 201 | + Assert.Contains("ExecuteTaskGetterCalls=2", observations); |
| 202 | + Assert.Contains("FirstRunResultCount=1", observations); |
| 203 | + Assert.Contains("FirstRunOutcome=Failed", observations); |
| 204 | + } |
| 205 | + finally |
| 206 | + { |
| 207 | + try |
| 208 | + { |
| 209 | + Directory.Delete(markerDirectory, recursive: true); |
| 210 | + } |
| 211 | + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) |
| 212 | + { |
| 213 | + // Best-effort cleanup of the temporary marker directory. |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + public sealed class TestAssetFixture() : TestAssetFixtureBase() |
| 219 | + { |
| 220 | + public const string ProjectName = "CustomRetryTests"; |
| 221 | + |
| 222 | + public const string MarkerDirectoryEnvVar = "CUSTOMRETRY_MARKER_DIR"; |
| 223 | + |
| 224 | + public const string MarkerFileName = "custom-retry.marker"; |
| 225 | + |
| 226 | + public string ProjectPath => GetAssetPath(ProjectName); |
| 227 | + |
| 228 | + public override (string ID, string Name, string Code) GetAssetsToGenerate() => (ProjectName, ProjectName, |
| 229 | + SourceCode |
| 230 | + .PatchTargetFrameworks(TargetFrameworks.NetCurrent) |
| 231 | + .PatchCodeWithReplace("$MSTestVersion$", MSTestVersion) |
| 232 | + .PatchCodeWithReplace("$MarkerDirectoryEnvVar$", MarkerDirectoryEnvVar) |
| 233 | + .PatchCodeWithReplace("$MarkerFileName$", MarkerFileName)); |
| 234 | + |
| 235 | + private const string SourceCode = """ |
| 236 | +#file CustomRetryTests.csproj |
| 237 | +<Project Sdk="Microsoft.NET.Sdk"> |
| 238 | +
|
| 239 | + <PropertyGroup> |
| 240 | + <OutputType>Exe</OutputType> |
| 241 | + <EnableMSTestRunner>true</EnableMSTestRunner> |
| 242 | + <TargetFrameworks>$TargetFrameworks$</TargetFrameworks> |
| 243 | + </PropertyGroup> |
| 244 | +
|
| 245 | + <ItemGroup> |
| 246 | + <PackageReference Include="MSTest.TestAdapter" Version="$MSTestVersion$" /> |
| 247 | + <PackageReference Include="MSTest.TestFramework" Version="$MSTestVersion$" /> |
| 248 | + </ItemGroup> |
| 249 | +</Project> |
| 250 | +
|
| 251 | +#file UnitTest1.cs |
| 252 | +#pragma warning disable MSTESTEXP |
| 253 | +
|
| 254 | +using System; |
| 255 | +using System.IO; |
| 256 | +using System.Threading.Tasks; |
| 257 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 258 | +
|
| 259 | +[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] |
| 260 | +public sealed class RetryTwiceAttribute : RetryBaseAttribute |
| 261 | +{ |
| 262 | + public static int ExecuteAsyncCalls { get; private set; } |
| 263 | +
|
| 264 | + public static int ExecuteTaskGetterCalls { get; private set; } |
| 265 | +
|
| 266 | + public static int FirstRunResultCount { get; private set; } |
| 267 | +
|
| 268 | + public static UnitTestOutcome FirstRunOutcome { get; private set; } |
| 269 | +
|
| 270 | + protected override async Task<RetryResult> ExecuteAsync(RetryContext retryContext) |
| 271 | + { |
| 272 | + ExecuteAsyncCalls++; |
| 273 | + FirstRunResultCount = retryContext.FirstRunResults.Length; |
| 274 | + FirstRunOutcome = retryContext.FirstRunResults[0].Outcome; |
| 275 | +
|
| 276 | + var retryResult = new RetryResult(); |
| 277 | + for (int i = 0; i < 2; i++) |
| 278 | + { |
| 279 | + ExecuteTaskGetterCalls++; |
| 280 | + retryResult.AddResult(await retryContext.ExecuteTaskGetter()); |
| 281 | + } |
| 282 | +
|
| 283 | + return retryResult; |
| 284 | + } |
| 285 | +} |
| 286 | +
|
| 287 | +[TestClass] |
| 288 | +public class CustomRetryTestClass |
| 289 | +{ |
| 290 | + private static int _testExecutions; |
| 291 | +
|
| 292 | + [TestMethod] |
| 293 | + [RetryTwice] |
| 294 | + public void FailsTwiceThenPasses() |
| 295 | + { |
| 296 | + _testExecutions++; |
| 297 | + if (_testExecutions < 3) |
| 298 | + { |
| 299 | + Assert.Fail($"Custom failure from execution {_testExecutions}"); |
| 300 | + } |
| 301 | + } |
| 302 | +
|
| 303 | + [ClassCleanup] |
| 304 | + public static void ClassCleanup() |
| 305 | + { |
| 306 | + string markerDirectory = Environment.GetEnvironmentVariable("$MarkerDirectoryEnvVar$"); |
| 307 | + if (string.IsNullOrEmpty(markerDirectory)) |
| 308 | + { |
| 309 | + return; |
| 310 | + } |
| 311 | +
|
| 312 | + File.WriteAllLines( |
| 313 | + Path.Combine(markerDirectory, "$MarkerFileName$"), |
| 314 | + new[] |
| 315 | + { |
| 316 | + $"TestExecutions={_testExecutions}", |
| 317 | + $"ExecuteAsyncCalls={RetryTwiceAttribute.ExecuteAsyncCalls}", |
| 318 | + $"ExecuteTaskGetterCalls={RetryTwiceAttribute.ExecuteTaskGetterCalls}", |
| 319 | + $"FirstRunResultCount={RetryTwiceAttribute.FirstRunResultCount}", |
| 320 | + $"FirstRunOutcome={RetryTwiceAttribute.FirstRunOutcome}", |
| 321 | + }); |
| 322 | + } |
| 323 | +} |
| 324 | +"""; |
| 325 | + } |
| 326 | + |
| 327 | + public TestContext TestContext { get; set; } |
| 328 | +} |
| 329 | + |
170 | 330 | [TestClass] |
171 | 331 | public sealed class ClassLevelRetryTests : AcceptanceTestBase<ClassLevelRetryTests.TestAssetFixture> |
172 | 332 | { |
|
0 commit comments