-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathRetryFailedTestsTests.cs
More file actions
481 lines (411 loc) · 21.8 KB
/
RetryFailedTestsTests.cs
File metadata and controls
481 lines (411 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#pragma warning disable IDE0073 // The file header does not match the required text
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under dual-license. See LICENSE.PLATFORMTOOLS.txt file in the project root for full license information.
#pragma warning restore IDE0073 // The file header does not match the required text
namespace Microsoft.Testing.Platform.Acceptance.IntegrationTests;
[TestClass]
public class RetryFailedTestsTests : AcceptanceTestBase<RetryFailedTestsTests.TestAssetFixture>
{
private const string AssetName = "RetryFailedTests";
internal static IEnumerable<(string Arguments, bool FailOnly)> GetMatrix()
{
foreach (string tfm in TargetFrameworks.All)
{
foreach (bool failOnly in new[] { true, false })
{
yield return (tfm, failOnly);
}
}
}
[TestMethod]
[DynamicData(nameof(GetMatrix))]
public async Task RetryFailedTests_OnlyRetryTimes_Succeeds(string tfm, bool failOnly)
{
var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, tfm);
string resultDirectory = Path.Combine(testHost.DirectoryName, Guid.NewGuid().ToString("N"));
TestHostResult testHostResult = await testHost.ExecuteAsync(
$"--retry-failed-tests 3 --results-directory {resultDirectory} --report-trx",
new()
{
{ EnvironmentVariableConstants.TESTINGPLATFORM_TELEMETRY_OPTOUT, "1" },
{ "METHOD1", "1" },
{ "FAIL", failOnly ? "1" : "0" },
{ "RESULTDIR", resultDirectory },
},
cancellationToken: TestContext.CancellationToken);
if (!failOnly)
{
testHostResult.AssertExitCodeIs(ExitCodes.Success);
testHostResult.AssertOutputContains("Tests suite completed successfully in 2 attempts");
testHostResult.AssertOutputContains("Failed! -");
testHostResult.AssertOutputContains("Passed! -");
string[] trxFiles = Directory.GetFiles(resultDirectory, "*.trx", SearchOption.AllDirectories);
Assert.HasCount(2, trxFiles);
string trxContents1 = File.ReadAllText(trxFiles[0]);
string trxContents2 = File.ReadAllText(trxFiles[1]);
Assert.AreNotEqual(trxContents1, trxContents2);
string id1 = Regex.Match(trxContents1, "<TestRun id=\"(.+?)\"").Groups[1].Value;
string id2 = Regex.Match(trxContents2, "<TestRun id=\"(.+?)\"").Groups[1].Value;
Assert.AreEqual(id1, id2);
}
else
{
testHostResult.AssertExitCodeIs(ExitCodes.AtLeastOneTestFailed);
testHostResult.AssertOutputContains("Tests suite failed in all 4 attempts");
testHostResult.AssertOutputContains("Tests suite failed, total failed tests: 1, exit code: 2, attempt: 1/4");
testHostResult.AssertOutputContains("Tests suite failed, total failed tests: 1, exit code: 2, attempt: 2/4");
testHostResult.AssertOutputContains("Tests suite failed, total failed tests: 1, exit code: 2, attempt: 3/4");
testHostResult.AssertOutputContains("Tests suite failed, total failed tests: 1, exit code: 2, attempt: 4/4");
testHostResult.AssertOutputDoesNotContain("Tests suite failed, total failed tests: 1, exit code: 2, attempt: 5/4");
testHostResult.AssertOutputContains("Failed! -");
}
}
[TestMethod]
[DynamicData(nameof(GetMatrix))]
public async Task RetryFailedTests_MaxPercentage_Succeeds(string tfm, bool fail)
{
var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, tfm);
string resultDirectory = Path.Combine(testHost.DirectoryName, Guid.NewGuid().ToString("N"));
TestHostResult testHostResult = await testHost.ExecuteAsync(
$"--retry-failed-tests 3 --retry-failed-tests-max-percentage 50 --results-directory {resultDirectory}",
new()
{
{ EnvironmentVariableConstants.TESTINGPLATFORM_TELEMETRY_OPTOUT, "1" },
{ "RESULTDIR", resultDirectory },
{ "METHOD1", "1" },
{ fail ? "METHOD2" : "UNUSED", "1" },
},
cancellationToken: TestContext.CancellationToken);
string retriesPath = Path.Combine(resultDirectory, "Retries");
Assert.IsTrue(Directory.Exists(retriesPath));
string[] retriesDirectories = Directory.GetDirectories(retriesPath);
Assert.HasCount(1, retriesDirectories);
string createdDirName = Path.GetFileName(retriesDirectories[0]);
// Asserts that we are not using long names, to reduce long path issues.
// See https://github.com/microsoft/testfx/issues/4002
Assert.AreEqual(5, createdDirName.Length, $"Expected directory '{createdDirName}' to be of length 5.");
if (fail)
{
testHostResult.AssertExitCodeIs(ExitCodes.AtLeastOneTestFailed);
testHostResult.AssertOutputContains("Failure threshold policy is enabled, failed tests will not be restarted.");
testHostResult.AssertOutputContains("Percentage failed threshold is 50% and 66.67% tests failed (2/3)");
testHostResult.AssertOutputContains("Failed! -");
}
else
{
testHostResult.AssertExitCodeIs(ExitCodes.Success);
testHostResult.AssertOutputContains("Tests suite completed successfully in 2 attempts");
testHostResult.AssertOutputContains("Failed! -");
testHostResult.AssertOutputContains("Passed! -");
}
}
[TestMethod]
[DynamicData(nameof(GetMatrix))]
public async Task RetryFailedTests_MaxTestsCount_Succeeds(string tfm, bool fail)
{
var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, tfm);
string resultDirectory = Path.Combine(testHost.DirectoryName, Guid.NewGuid().ToString("N"));
TestHostResult testHostResult = await testHost.ExecuteAsync(
$"--retry-failed-tests 3 --retry-failed-tests-max-tests 1 --results-directory {resultDirectory}",
new()
{
{ EnvironmentVariableConstants.TESTINGPLATFORM_TELEMETRY_OPTOUT, "1" },
{ "RESULTDIR", resultDirectory },
{ "METHOD1", "1" },
{ fail ? "METHOD2" : "UNUSED", "1" },
}, cancellationToken: TestContext.CancellationToken);
if (fail)
{
testHostResult.AssertExitCodeIs(ExitCodes.AtLeastOneTestFailed);
testHostResult.AssertOutputContains("Failure threshold policy is enabled, failed tests will not be restarted.");
testHostResult.AssertOutputContains("Maximum failed tests threshold is 1 and 2 tests failed");
testHostResult.AssertOutputContains("Failed! -");
}
else
{
testHostResult.AssertExitCodeIs(ExitCodes.Success);
testHostResult.AssertOutputContains("Tests suite completed successfully in 2 attempts");
testHostResult.AssertOutputContains("Failed! -");
testHostResult.AssertOutputContains("Passed! -");
}
}
[TestMethod]
// We use crash dump, not supported in NetFramework at the moment
[DynamicData(nameof(TargetFrameworks.NetForDynamicData), typeof(TargetFrameworks))]
public async Task RetryFailedTests_MoveFiles_Succeeds(string tfm)
{
var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, tfm);
string resultDirectory = Path.Combine(testHost.DirectoryName, Guid.NewGuid().ToString("N"));
TestHostResult testHostResult = await testHost.ExecuteAsync(
$"--report-trx --crashdump --retry-failed-tests 1 --results-directory {resultDirectory}",
new()
{
{ EnvironmentVariableConstants.TESTINGPLATFORM_TELEMETRY_OPTOUT, "1" },
{ "RESULTDIR", resultDirectory },
{ "CRASH", "1" },
},
cancellationToken: TestContext.CancellationToken);
testHostResult.AssertExitCodeIs(ExitCodes.TestHostProcessExitedNonGracefully);
string[] entries = [.. Directory.GetFiles(resultDirectory, "*.*", SearchOption.AllDirectories).Where(x => !x.Contains("Retries", StringComparison.OrdinalIgnoreCase))];
// 1 trx file
Assert.ContainsSingle(x => x.EndsWith("trx", StringComparison.OrdinalIgnoreCase), entries);
// Number of dmp files seems to differ locally and in CI
int dumpFilesCount = entries.Count(x => x.EndsWith("dmp", StringComparison.OrdinalIgnoreCase));
if (dumpFilesCount == 2)
{
// Dump file inside the trx structure
Assert.ContainsSingle(x => x.Contains($"{Path.DirectorySeparatorChar}In{Path.DirectorySeparatorChar}", StringComparison.OrdinalIgnoreCase) && x.EndsWith("dmp", StringComparison.OrdinalIgnoreCase), entries);
}
else if (dumpFilesCount is 0 or > 2)
{
Assert.Fail($"Expected 1 or 2 dump files, but found {dumpFilesCount}");
}
}
[TestMethod]
public async Task RetryFailedTests_PassingFromFirstTime_UsingTestTarget_MoveFiles_Succeeds()
{
string resultDirectory = Path.Combine(AssetFixture.TargetAssetPath, Guid.NewGuid().ToString("N"));
DotnetMuxerResult result = await DotnetCli.RunAsync(
$"build \"{AssetFixture.TargetAssetPath}\" -t:Test -p:TestingPlatformCommandLineArguments=\"--retry-failed-tests 1 --results-directory %22{resultDirectory}%22\"",
AcceptanceFixture.NuGetGlobalPackagesFolder.Path,
workingDirectory: AssetFixture.TargetAssetPath, cancellationToken: TestContext.CancellationToken);
result.AssertExitCodeIs(ExitCodes.Success);
// File names are on the form: RetryFailedTests_tfm_architecture.log
string[] logFilesFromInvokeTestingPlatformTask = Directory.GetFiles(resultDirectory, "RetryFailedTests_*_*.log");
Assert.HasCount(TargetFrameworks.All.Length, logFilesFromInvokeTestingPlatformTask);
foreach (string logFile in logFilesFromInvokeTestingPlatformTask)
{
string logFileContents = File.ReadAllText(logFile);
Assert.Contains("Test run summary: Passed!", logFileContents);
Assert.Contains("total: 3", logFileContents);
Assert.Contains("succeeded: 3", logFileContents);
Assert.Contains("Tests suite completed successfully in 1 attempts", logFileContents);
}
}
[TestMethod]
[DynamicData(nameof(TargetFrameworks.NetForDynamicData), typeof(TargetFrameworks))]
public async Task RetryFailedTests_WithPreexistingFilterUid_ReplacesFilterOnRetry(string tfm)
{
var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, tfm);
string resultDirectory = Path.Combine(testHost.DirectoryName, Guid.NewGuid().ToString("N"));
// Use --filter-uid to select tests 1 and 2. Test 1 will fail on first attempt, pass on second.
TestHostResult testHostResult = await testHost.ExecuteAsync(
$"--retry-failed-tests 3 --filter-uid 1 --filter-uid 2 --results-directory {resultDirectory}",
new()
{
{ EnvironmentVariableConstants.TESTINGPLATFORM_TELEMETRY_OPTOUT, "1" },
{ "METHOD1", "1" },
{ "RESULTDIR", resultDirectory },
},
cancellationToken: TestContext.CancellationToken);
testHostResult.AssertExitCodeIs(ExitCodes.Success);
testHostResult.AssertOutputContains("Tests suite completed successfully in 2 attempts");
// The retry attempt should only retry the failed test (UID 1), not all originally filtered tests.
testHostResult.AssertOutputContains("Tests suite failed, total failed tests: 1, exit code: 2, attempt: 1/4");
}
[TestMethod]
[DynamicData(nameof(TargetFrameworks.NetForDynamicData), typeof(TargetFrameworks))]
public async Task RetryFailedTests_WithPreexistingTreenodeFilter_ReplacesFilterOnRetry(string tfm)
{
var testHost = TestInfrastructure.TestHost.LocateFrom(AssetFixture.TargetAssetPath, AssetName, tfm);
string resultDirectory = Path.Combine(testHost.DirectoryName, Guid.NewGuid().ToString("N"));
// Use --treenode-filter to select all tests. Test 1 will fail on first attempt, pass on second.
TestHostResult testHostResult = await testHost.ExecuteAsync(
$"--retry-failed-tests 3 --treenode-filter /** --results-directory {resultDirectory}",
new()
{
{ EnvironmentVariableConstants.TESTINGPLATFORM_TELEMETRY_OPTOUT, "1" },
{ "METHOD1", "1" },
{ "RESULTDIR", resultDirectory },
},
cancellationToken: TestContext.CancellationToken);
testHostResult.AssertExitCodeIs(ExitCodes.Success);
testHostResult.AssertOutputContains("Tests suite completed successfully in 2 attempts");
// The retry attempt should only retry the failed test (UID 1), not all tests matching the tree filter.
testHostResult.AssertOutputContains("Tests suite failed, total failed tests: 1, exit code: 2, attempt: 1/4");
}
public sealed class TestAssetFixture() : TestAssetFixtureBase(AcceptanceFixture.NuGetGlobalPackagesFolder)
{
public string TargetAssetPath => GetAssetPath(AssetName);
public override IEnumerable<(string ID, string Name, string Code)> GetAssetsToGenerate()
{
yield return (AssetName, AssetName,
TestCode
.PatchTargetFrameworks(TargetFrameworks.All)
.PatchCodeWithReplace("$MicrosoftTestingPlatformVersion$", MicrosoftTestingPlatformVersion));
}
private const string TestCode = """
#file RetryFailedTests.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$TargetFrameworks$</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
<LangVersion>preview</LangVersion>
<GenerateTestingPlatformEntryPoint>false</GenerateTestingPlatformEntryPoint>
<TestingPlatformCaptureOutput>false</TestingPlatformCaptureOutput>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Testing.Extensions.CrashDump" Version="$MicrosoftTestingPlatformVersion$" />
<PackageReference Include="Microsoft.Testing.Extensions.Retry" Version="$MicrosoftTestingPlatformVersion$" />
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="$MicrosoftTestingPlatformVersion$" />
<PackageReference Include="Microsoft.Testing.Platform.MSBuild" Version="$MicrosoftTestingPlatformVersion$" />
</ItemGroup>
</Project>
#file global.json
{
"test": {
"runner": "VSTest"
}
}
#file Program.cs
using Microsoft.Testing.Extensions;
using Microsoft.Testing.Extensions.TrxReport.Abstractions;
using Microsoft.Testing.Platform.Builder;
using Microsoft.Testing.Platform.Capabilities.TestFramework;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Extensions.TestFramework;
using Microsoft.Testing.Platform.MSBuild;
using Microsoft.Testing.Platform.Requests;
using Microsoft.Testing.Platform.Services;
public class Program
{
public static async Task<int> Main(string[] args)
{
ITestApplicationBuilder builder = await TestApplication.CreateBuilderAsync(args);
builder.RegisterTestFramework(
sp => new TestFrameworkCapabilities(new TrxReportCapability()),
(_,__) => new DummyTestFramework());
builder.AddCrashDumpProvider();
builder.AddTrxReportProvider();
builder.AddRetryProvider();
builder.AddMSBuild();
using ITestApplication app = await builder.BuildAsync();
return await app.RunAsync();
}
}
public class TrxReportCapability : ITrxReportCapability
{
bool ITrxReportCapability.IsSupported { get; } = true;
void ITrxReportCapability.Enable()
{
}
}
public class DummyTestFramework : ITestFramework, IDataProducer
{
public string Uid => nameof(DummyTestFramework);
public string Version => "2.0.0";
public string DisplayName => nameof(DummyTestFramework);
public string Description => nameof(DummyTestFramework);
public Type[] DataTypesProduced => new[] { typeof(TestNodeUpdateMessage) };
public Task<bool> IsEnabledAsync() => Task.FromResult(true);
public Task<CreateTestSessionResult> CreateTestSessionAsync(CreateTestSessionContext context)
=> Task.FromResult(new CreateTestSessionResult() { IsSuccess = true });
public Task<CloseTestSessionResult> CloseTestSessionAsync(CloseTestSessionContext context)
=> Task.FromResult(new CloseTestSessionResult() { IsSuccess = true });
public async Task ExecuteRequestAsync(ExecuteRequestContext context)
{
bool fail = Environment.GetEnvironmentVariable("FAIL") == "1";
// Tests are using this env variable so it won't be null.
string resultDir = Environment.GetEnvironmentVariable("RESULTDIR")!;
bool crash = Environment.GetEnvironmentVariable("CRASH") == "1";
var uidFilter = (context.Request as TestExecutionRequest)?.Filter as TestNodeUidListFilter;
var testMethod1Identifier = new TestMethodIdentifierProperty(string.Empty, string.Empty, "DummyClassName", "TestMethod1", 0, Array.Empty<string>(), string.Empty);
var testMethod2Identifier = new TestMethodIdentifierProperty(string.Empty, string.Empty, "DummyClassName", "TestMethod2", 0, Array.Empty<string>(), string.Empty);
var testMethod3Identifier = new TestMethodIdentifierProperty(string.Empty, string.Empty, "DummyClassName", "TestMethod3", 0, Array.Empty<string>(), string.Empty);
if (IsIncluded(uidFilter, "1"))
{
if (TestMethod1(fail, resultDir, crash))
{
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid,
new TestNode() { Uid = "1", DisplayName = "TestMethod1", Properties = new(PassedTestNodeStateProperty.CachedInstance, testMethod1Identifier) }));
}
else
{
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid,
new TestNode() { Uid = "1", DisplayName = "TestMethod1", Properties = new(new FailedTestNodeStateProperty(), testMethod1Identifier) }));
}
}
if (IsIncluded(uidFilter, "2"))
{
if (TestMethod2(fail, resultDir))
{
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid,
new TestNode() { Uid = "2", DisplayName = "TestMethod2", Properties = new(PassedTestNodeStateProperty.CachedInstance, testMethod2Identifier) }));
}
else
{
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid,
new TestNode() { Uid = "2", DisplayName = "TestMethod2", Properties = new(new FailedTestNodeStateProperty(), testMethod2Identifier) }));
}
}
if (IsIncluded(uidFilter, "3"))
{
if (TestMethod3(fail, resultDir))
{
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid,
new TestNode() { Uid = "3", DisplayName = "TestMethod3", Properties = new(PassedTestNodeStateProperty.CachedInstance, testMethod3Identifier) }));
}
else
{
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(context.Request.Session.SessionUid,
new TestNode() { Uid = "3", DisplayName = "TestMethod3", Properties = new(new FailedTestNodeStateProperty(), testMethod3Identifier) }));
}
}
context.Complete();
}
private static bool IsIncluded(TestNodeUidListFilter? filter, string uid)
=> filter is null || filter.TestNodeUids.Any(n => n.Value == uid);
private bool TestMethod1(bool fail, string resultDir, bool crash)
{
if (crash)
{
Environment.FailFast("CRASH");
}
bool envVar = Environment.GetEnvironmentVariable("METHOD1") is null;
if (envVar) return true;
string succeededFile = Path.Combine(resultDir, "M1_Succeeds");
bool fileExits = File.Exists(succeededFile);
bool assert = envVar && fileExits;
if (!fail)
{
if (fileExits) return true;
if (!assert) File.WriteAllText(succeededFile,"");
}
return assert;
}
private bool TestMethod2(bool fail, string resultDir)
{
bool envVar = Environment.GetEnvironmentVariable("METHOD2") is null;
System.Console.WriteLine("envVar " + envVar);
if (envVar) return true;
string succeededFile = Path.Combine(resultDir,"M2_Succeeds");
bool fileExits = File.Exists(succeededFile);
bool assert = envVar && fileExits;
if (!fail)
{
if (fileExits) return true;
if (!assert) File.WriteAllText(succeededFile,"");
}
return assert;
}
private bool TestMethod3(bool fail, string resultDir)
{
bool envVar = Environment.GetEnvironmentVariable("METHOD3") is null;
if (envVar) return true;
string succeededFile = Path.Combine(resultDir,"M3_Succeeds");
bool fileExits = File.Exists(succeededFile);
bool assert = envVar && fileExits;
if (!fail)
{
if (fileExits) return true;
if (!assert) File.WriteAllText(succeededFile,"");
}
return assert;
}
}
""";
}
public TestContext TestContext { get; set; }
}