From 49599419d351132489cacfed32968dcb4fc1b84a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Tue, 10 Sep 2024 11:25:04 +0200 Subject: [PATCH] Add option to order tests by name (#3796) --- .../Execution/TestExecutionManager.cs | 6 ++- .../MSTest.TestAdapter/MSTestSettings.cs | 52 +++++++++++++------ 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/Adapter/MSTest.TestAdapter/Execution/TestExecutionManager.cs b/src/Adapter/MSTest.TestAdapter/Execution/TestExecutionManager.cs index cb43057daf..01eb58ad7f 100644 --- a/src/Adapter/MSTest.TestAdapter/Execution/TestExecutionManager.cs +++ b/src/Adapter/MSTest.TestAdapter/Execution/TestExecutionManager.cs @@ -407,7 +407,11 @@ private void ExecuteTestsWithTestRunner( bool hasAnyRunnableTests = false; var fixtureTests = new List(); - foreach (TestCase currentTest in tests) + IEnumerable orderedTests = MSTestSettings.CurrentSettings.OrderTestsByNameInClass + ? tests.OrderBy(t => t.GetManagedType()).ThenBy(t => t.GetManagedMethod()) + : tests; + + foreach (TestCase currentTest in orderedTests) { _testRunCancellationToken?.ThrowIfCancellationRequested(); diff --git a/src/Adapter/MSTest.TestAdapter/MSTestSettings.cs b/src/Adapter/MSTest.TestAdapter/MSTestSettings.cs index ec2e5d33cf..3825cd9506 100644 --- a/src/Adapter/MSTest.TestAdapter/MSTestSettings.cs +++ b/src/Adapter/MSTest.TestAdapter/MSTestSettings.cs @@ -66,6 +66,7 @@ public MSTestSettings() TestCleanupTimeout = 0; TreatClassAndAssemblyCleanupWarningsAsErrors = false; CooperativeCancellationTimeout = false; + OrderTestsByNameInClass = false; } /// @@ -204,6 +205,11 @@ public static RunConfigurationSettings RunConfigurationSettings /// internal bool CooperativeCancellationTimeout { get; private set; } + /// + /// Gets a value indicating whether tests should be ordered by name in the class. + /// + internal bool OrderTestsByNameInClass { get; private set; } + /// /// Populate settings based on existing settings object. /// @@ -215,28 +221,29 @@ public static void PopulateSettings(MSTestSettings settings) return; } + CurrentSettings.AssemblyCleanupTimeout = settings.AssemblyCleanupTimeout; + CurrentSettings.AssemblyInitializeTimeout = settings.AssemblyInitializeTimeout; CurrentSettings.CaptureDebugTraces = settings.CaptureDebugTraces; + CurrentSettings.ClassCleanupLifecycle = settings.ClassCleanupLifecycle; + CurrentSettings.ClassCleanupTimeout = settings.ClassCleanupTimeout; + CurrentSettings.ClassInitializeTimeout = settings.ClassInitializeTimeout; + CurrentSettings.ConsiderEmptyDataSourceAsInconclusive = settings.ConsiderEmptyDataSourceAsInconclusive; + CurrentSettings.ConsiderFixturesAsSpecialTests = settings.ConsiderFixturesAsSpecialTests; + CurrentSettings.CooperativeCancellationTimeout = settings.CooperativeCancellationTimeout; + CurrentSettings.DisableParallelization = settings.DisableParallelization; + CurrentSettings.EnableBaseClassTestMethodsFromOtherAssemblies = settings.EnableBaseClassTestMethodsFromOtherAssemblies; CurrentSettings.ForcedLegacyMode = settings.ForcedLegacyMode; - CurrentSettings.TestSettingsFile = settings.TestSettingsFile; CurrentSettings.MapInconclusiveToFailed = settings.MapInconclusiveToFailed; CurrentSettings.MapNotRunnableToFailed = settings.MapNotRunnableToFailed; - CurrentSettings.TreatDiscoveryWarningsAsErrors = settings.TreatDiscoveryWarningsAsErrors; - CurrentSettings.EnableBaseClassTestMethodsFromOtherAssemblies = settings.EnableBaseClassTestMethodsFromOtherAssemblies; - CurrentSettings.ClassCleanupLifecycle = settings.ClassCleanupLifecycle; - CurrentSettings.ParallelizationWorkers = settings.ParallelizationWorkers; + CurrentSettings.OrderTestsByNameInClass = settings.OrderTestsByNameInClass; CurrentSettings.ParallelizationScope = settings.ParallelizationScope; - CurrentSettings.DisableParallelization = settings.DisableParallelization; + CurrentSettings.ParallelizationWorkers = settings.ParallelizationWorkers; + CurrentSettings.TestCleanupTimeout = settings.TestCleanupTimeout; + CurrentSettings.TestInitializeTimeout = settings.TestInitializeTimeout; + CurrentSettings.TestSettingsFile = settings.TestSettingsFile; CurrentSettings.TestTimeout = settings.TestTimeout; CurrentSettings.TreatClassAndAssemblyCleanupWarningsAsErrors = settings.TreatClassAndAssemblyCleanupWarningsAsErrors; - CurrentSettings.AssemblyInitializeTimeout = settings.AssemblyInitializeTimeout; - CurrentSettings.AssemblyCleanupTimeout = settings.AssemblyCleanupTimeout; - CurrentSettings.ConsiderEmptyDataSourceAsInconclusive = settings.ConsiderEmptyDataSourceAsInconclusive; - CurrentSettings.ClassInitializeTimeout = settings.ClassInitializeTimeout; - CurrentSettings.ClassCleanupTimeout = settings.ClassCleanupTimeout; - CurrentSettings.TestInitializeTimeout = settings.TestInitializeTimeout; - CurrentSettings.TestCleanupTimeout = settings.TestCleanupTimeout; - CurrentSettings.ConsiderFixturesAsSpecialTests = settings.ConsiderFixturesAsSpecialTests; - CurrentSettings.CooperativeCancellationTimeout = settings.CooperativeCancellationTimeout; + CurrentSettings.TreatDiscoveryWarningsAsErrors = settings.TreatDiscoveryWarningsAsErrors; } /// @@ -693,6 +700,21 @@ private static MSTestSettings ToSettings(XmlReader reader, IMessageLogger? logge break; } + case "ORDERTESTSBYNAMEINCLASS": + { + string value = reader.ReadInnerXml(); + if (bool.TryParse(value, out result)) + { + settings.OrderTestsByNameInClass = result; + } + else + { + logger?.SendMessage(TestMessageLevel.Warning, string.Format(CultureInfo.CurrentCulture, Resource.InvalidValue, value, "OrderTestsByNameInClass")); + } + + break; + } + default: { PlatformServiceProvider.Instance.SettingsProvider.Load(reader.ReadSubtree());