-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support '--maximum-failed-tests' to abort test run when failure thres…
…hold is reached (#4238)
- Loading branch information
1 parent
19b63cd
commit 17e322a
Showing
41 changed files
with
1,105 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...er/MSTest.TestAdapter/TestingPlatformAdapter/MSTestGracefulStopTestExecutionCapability.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.Testing.Platform.Capabilities.TestFramework; | ||
|
||
namespace Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
#pragma warning disable TPEXP // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. | ||
internal sealed class MSTestGracefulStopTestExecutionCapability : IGracefulStopTestExecutionCapability | ||
#pragma warning restore TPEXP // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. | ||
{ | ||
private MSTestGracefulStopTestExecutionCapability() | ||
{ | ||
} | ||
|
||
public static MSTestGracefulStopTestExecutionCapability Instance { get; } = new(); | ||
|
||
public bool IsStopRequested { get; private set; } | ||
|
||
public Task StopTestExecutionAsync(CancellationToken cancellationToken) | ||
{ | ||
IsStopRequested = true; | ||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...osoft.Testing.Platform/Capabilities/TestFramework/IGracefulStopTestExecutionCapability.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Microsoft.Testing.Platform.Capabilities.TestFramework; | ||
|
||
/// <summary> | ||
/// A capability to support stopping test execution gracefully, without cancelling/aborting everything. | ||
/// This is used to support '--maximum-failed-tests'. | ||
/// </summary> | ||
/// <remarks> | ||
/// Test frameworks can choose to run any needed cleanup when cancellation is requested. | ||
/// </remarks> | ||
[Experimental("TPEXP", UrlFormat = "https://aka.ms/testingplatform/diagnostics#{0}")] | ||
public interface IGracefulStopTestExecutionCapability : ITestFrameworkCapability | ||
{ | ||
Task StopTestExecutionAsync(CancellationToken cancellationToken); | ||
} |
53 changes: 53 additions & 0 deletions
53
...atform/Microsoft.Testing.Platform/CommandLine/MaxFailedTestsCommandLineOptionsProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Globalization; | ||
|
||
using Microsoft.Testing.Platform.Extensions; | ||
using Microsoft.Testing.Platform.Extensions.CommandLine; | ||
using Microsoft.Testing.Platform.Helpers; | ||
using Microsoft.Testing.Platform.Resources; | ||
|
||
namespace Microsoft.Testing.Platform.CommandLine; | ||
|
||
internal sealed class MaxFailedTestsCommandLineOptionsProvider(IExtension extension) : ICommandLineOptionsProvider | ||
{ | ||
internal const string MaxFailedTestsOptionKey = "maximum-failed-tests"; | ||
|
||
private static readonly IReadOnlyCollection<CommandLineOption> OptionsCache = | ||
[ | ||
new(MaxFailedTestsOptionKey, PlatformResources.PlatformCommandLineMaxFailedTestsOptionDescription, ArgumentArity.ExactlyOne, isHidden: false), | ||
]; | ||
|
||
public string Uid => extension.Uid; | ||
|
||
public string Version => extension.Version; | ||
|
||
public string DisplayName => extension.DisplayName; | ||
|
||
public string Description => extension.Description; | ||
|
||
public IReadOnlyCollection<CommandLineOption> GetCommandLineOptions() | ||
=> OptionsCache; | ||
|
||
public Task<bool> IsEnabledAsync() => Task.FromResult(true); | ||
|
||
public Task<ValidationResult> ValidateCommandLineOptionsAsync(ICommandLineOptions commandLineOptions) | ||
=> ValidationResult.ValidTask; | ||
|
||
public Task<ValidationResult> ValidateOptionArgumentsAsync(CommandLineOption commandOption, string[] arguments) | ||
{ | ||
if (commandOption.Name == MaxFailedTestsOptionKey) | ||
{ | ||
string arg = arguments[0]; | ||
// We consider --maximum-failed-tests 0 as invalid. | ||
// The idea is that we stop the execution when we *reach* the max failed tests, not when *exceed*. | ||
// So the value 1 means, stop execution on the first failure. | ||
return int.TryParse(arg, out int maxFailedTestsResult) && maxFailedTestsResult > 0 | ||
? ValidationResult.ValidTask | ||
: ValidationResult.InvalidTask(string.Format(CultureInfo.InvariantCulture, PlatformResources.MaxFailedTestsMustBePositive, arg)); | ||
} | ||
|
||
throw ApplicationStateGuard.Unreachable(); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/Platform/Microsoft.Testing.Platform/Extensions/AbortForMaxFailedTestsExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.Testing.Platform.Capabilities.TestFramework; | ||
using Microsoft.Testing.Platform.CommandLine; | ||
using Microsoft.Testing.Platform.Extensions.Messages; | ||
using Microsoft.Testing.Platform.Extensions.TestHost; | ||
using Microsoft.Testing.Platform.Helpers; | ||
using Microsoft.Testing.Platform.Messages; | ||
using Microsoft.Testing.Platform.Resources; | ||
using Microsoft.Testing.Platform.Services; | ||
|
||
namespace Microsoft.Testing.Platform.Extensions; | ||
|
||
internal sealed class AbortForMaxFailedTestsExtension : IDataConsumer | ||
{ | ||
private readonly int? _maxFailedTests; | ||
private readonly IGracefulStopTestExecutionCapability? _capability; | ||
private readonly IStopPoliciesService _policiesService; | ||
private readonly ITestApplicationCancellationTokenSource _testApplicationCancellationTokenSource; | ||
private int _failCount; | ||
|
||
public AbortForMaxFailedTestsExtension( | ||
ICommandLineOptions commandLineOptions, | ||
IGracefulStopTestExecutionCapability? capability, | ||
IStopPoliciesService policiesService, | ||
ITestApplicationCancellationTokenSource testApplicationCancellationTokenSource) | ||
{ | ||
if (commandLineOptions.TryGetOptionArgumentList(MaxFailedTestsCommandLineOptionsProvider.MaxFailedTestsOptionKey, out string[]? args) && | ||
int.TryParse(args[0], out int maxFailedTests) && | ||
maxFailedTests > 0) | ||
{ | ||
_maxFailedTests = maxFailedTests; | ||
} | ||
|
||
_capability = capability; | ||
_policiesService = policiesService; | ||
_testApplicationCancellationTokenSource = testApplicationCancellationTokenSource; | ||
} | ||
|
||
public Type[] DataTypesConsumed { get; } = [typeof(TestNodeUpdateMessage)]; | ||
|
||
/// <inheritdoc /> | ||
public string Uid { get; } = nameof(AbortForMaxFailedTestsExtension); | ||
|
||
/// <inheritdoc /> | ||
public string Version { get; } = AppVersion.DefaultSemVer; | ||
|
||
/// <inheritdoc /> | ||
public string DisplayName { get; } = nameof(AbortForMaxFailedTestsExtension); | ||
|
||
/// <inheritdoc /> | ||
public string Description { get; } = PlatformResources.AbortForMaxFailedTestsDescription; | ||
|
||
/// <inheritdoc /> | ||
public Task<bool> IsEnabledAsync() => Task.FromResult(_maxFailedTests.HasValue && _capability is not null); | ||
|
||
public async Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationToken cancellationToken) | ||
{ | ||
var node = (TestNodeUpdateMessage)value; | ||
|
||
// If we are called, the extension is enabled, which means both _maxFailedTests and capability are not null. | ||
RoslynDebug.Assert(_maxFailedTests is not null); | ||
RoslynDebug.Assert(_capability is not null); | ||
|
||
TestNodeStateProperty testNodeStateProperty = node.TestNode.Properties.Single<TestNodeStateProperty>(); | ||
if (TestNodePropertiesCategories.WellKnownTestNodeTestRunOutcomeFailedProperties.Any(t => t == testNodeStateProperty.GetType()) && | ||
++_failCount >= _maxFailedTests.Value && | ||
// If already triggered, don't do it again. | ||
!_policiesService.IsMaxFailedTestsTriggered) | ||
{ | ||
await _capability.StopTestExecutionAsync(_testApplicationCancellationTokenSource.CancellationToken); | ||
await _policiesService.ExecuteMaxFailedTestsCallbacksAsync(_maxFailedTests.Value, _testApplicationCancellationTokenSource.CancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.