-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support specific/custom test orders and a deterministic order in whic…
…h test classes are executed. (#1851) * Add support for executing tests in a specific order. The specific order is only supported within a class for now. * Add support for ordering test classes.
- Loading branch information
Showing
10 changed files
with
232 additions
and
8 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
9 changes: 9 additions & 0 deletions
9
test/EFCore.MySql.FunctionalTests/TestUtilities/Xunit/IMySqlTestClassOrderer.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,9 @@ | ||
using System.Collections.Generic; | ||
using Xunit.Abstractions; | ||
|
||
namespace Pomelo.EntityFrameworkCore.MySql.FunctionalTests.TestUtilities.Xunit; | ||
|
||
public interface IMySqlTestClassOrderer | ||
{ | ||
IEnumerable<ITestClass> OrderTestClasses(IEnumerable<ITestClass> testClasses); | ||
} |
40 changes: 38 additions & 2 deletions
40
test/EFCore.MySql.FunctionalTests/TestUtilities/Xunit/MySqlTestCaseOrderer.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 |
---|---|---|
@@ -1,15 +1,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using Xunit.Abstractions; | ||
using Xunit.Sdk; | ||
|
||
namespace Pomelo.EntityFrameworkCore.MySql.FunctionalTests.TestUtilities.Xunit | ||
{ | ||
public class MySqlTestCaseOrderer : ITestCaseOrderer | ||
public class MySqlTestCaseOrderer : ITestCaseOrderer, IMySqlTestClassOrderer | ||
{ | ||
#if SPECIFIC_TEST_ORDER | ||
private static readonly bool _isSpecificTestCaseOrderingEnabled = true; | ||
#else | ||
private static readonly bool _isSpecificTestCaseOrderingEnabled = false; | ||
#endif | ||
|
||
private static string[] _specificTestCaseDisplayNamesInOrder; | ||
public static string[] SpecificTestCaseDisplayNamesInOrder | ||
=> _specificTestCaseDisplayNamesInOrder ??= _isSpecificTestCaseOrderingEnabled && | ||
Path.GetFullPath(@"..\..\..\TestResults\SpecificTestOrder.txt") is var path && | ||
File.Exists(path) | ||
? File.ReadLines(path) | ||
.Select(s => Regex.Match(s, @"^(?:\W*)([^\u200B]+)").Groups[1].Value) | ||
.Where(s => !string.IsNullOrEmpty(s)) | ||
.Distinct() | ||
.ToArray() | ||
: []; | ||
|
||
private readonly Dictionary<string, int> _specificTestCaseDisplayNamesWithIndex; | ||
|
||
public MySqlTestCaseOrderer() | ||
{ | ||
_specificTestCaseDisplayNamesWithIndex = SpecificTestCaseDisplayNamesInOrder | ||
.Select((s, i) => (s, i)) | ||
.ToDictionary(t => t.s, t => t.i); | ||
} | ||
|
||
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases) | ||
where TTestCase : ITestCase | ||
=> testCases.OrderBy(c => c.TestMethod.Method.Name, StringComparer.OrdinalIgnoreCase); | ||
=> testCases | ||
.OrderBy(c => _specificTestCaseDisplayNamesWithIndex.GetValueOrDefault(c.DisplayName, int.MaxValue)) | ||
.ThenBy(c => c.DisplayName, StringComparer.OrdinalIgnoreCase) | ||
.ThenBy(c => c.DisplayName, StringComparer.Ordinal) | ||
.ThenBy(c => c.UniqueID); | ||
|
||
public IEnumerable<ITestClass> OrderTestClasses(IEnumerable<ITestClass> testClasses) | ||
=> testClasses.OrderBy(c => c.Class.Name); | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
test/EFCore.MySql.FunctionalTests/TestUtilities/Xunit/MySqlXunitTestAssemblyRunner.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,34 @@ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit.Abstractions; | ||
using Xunit.Sdk; | ||
|
||
namespace Pomelo.EntityFrameworkCore.MySql.FunctionalTests.TestUtilities.Xunit; | ||
|
||
public class MySqlXunitTestAssemblyRunner : XunitTestAssemblyRunner | ||
{ | ||
public MySqlXunitTestAssemblyRunner( | ||
ITestAssembly testAssembly, | ||
IEnumerable<IXunitTestCase> testCases, | ||
IMessageSink diagnosticMessageSink, | ||
IMessageSink executionMessageSink, | ||
ITestFrameworkExecutionOptions executionOptions) | ||
: base(testAssembly, testCases, diagnosticMessageSink, executionMessageSink, executionOptions) | ||
{ | ||
} | ||
|
||
protected override Task<RunSummary> RunTestCollectionAsync( | ||
IMessageBus messageBus, | ||
ITestCollection testCollection, | ||
IEnumerable<IXunitTestCase> testCases, | ||
CancellationTokenSource cancellationTokenSource) | ||
=> new MySqlXunitTestCollectionRunner( | ||
testCollection, | ||
testCases, | ||
DiagnosticMessageSink, | ||
messageBus, | ||
TestCaseOrderer, | ||
new ExceptionAggregator(Aggregator), | ||
cancellationTokenSource).RunAsync(); | ||
} |
51 changes: 51 additions & 0 deletions
51
test/EFCore.MySql.FunctionalTests/TestUtilities/Xunit/MySqlXunitTestCollectionRunner.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,51 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit.Abstractions; | ||
using Xunit.Sdk; | ||
|
||
namespace Pomelo.EntityFrameworkCore.MySql.FunctionalTests.TestUtilities.Xunit; | ||
|
||
public class MySqlXunitTestCollectionRunner : XunitTestCollectionRunner | ||
{ | ||
public MySqlXunitTestCollectionRunner( | ||
ITestCollection testCollection, | ||
IEnumerable<IXunitTestCase> testCases, | ||
IMessageSink diagnosticMessageSink, | ||
IMessageBus messageBus, | ||
ITestCaseOrderer testCaseOrderer, | ||
ExceptionAggregator aggregator, | ||
CancellationTokenSource cancellationTokenSource) | ||
: base(testCollection, testCases, diagnosticMessageSink, messageBus, testCaseOrderer, aggregator, cancellationTokenSource) | ||
{ | ||
} | ||
|
||
protected override async Task<RunSummary> RunTestClassesAsync() | ||
{ | ||
var summary = new RunSummary(); | ||
|
||
var testsGroupedByClass = TestCases.GroupBy(tc => tc.TestMethod.TestClass, TestClassComparer.Instance); | ||
|
||
// Explicitly order test classes. | ||
if (TestCaseOrderer is IMySqlTestClassOrderer testClassOrderer) | ||
{ | ||
var testClassesWithIndex = testClassOrderer | ||
.OrderTestClasses(testsGroupedByClass.Select(g => g.Key)) | ||
.Select((s, i) => (s, i)) | ||
.ToDictionary(t => t.s, t => t.i); | ||
|
||
testsGroupedByClass = testsGroupedByClass | ||
.OrderBy(g => testClassesWithIndex.GetValueOrDefault(g.Key, int.MaxValue)); | ||
} | ||
|
||
foreach (var testCasesByClass in testsGroupedByClass) | ||
{ | ||
summary.Aggregate(await RunTestClassAsync(testCasesByClass.Key, (IReflectionTypeInfo)testCasesByClass.Key.Class, testCasesByClass)); | ||
if (CancellationTokenSource.IsCancellationRequested) | ||
break; | ||
} | ||
|
||
return summary; | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
test/EFCore.MySql.FunctionalTests/TestUtilities/Xunit/MySqlXunitTestFrameworkExecutor.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,31 @@ | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using Xunit.Abstractions; | ||
using Xunit.Sdk; | ||
|
||
namespace Pomelo.EntityFrameworkCore.MySql.FunctionalTests.TestUtilities.Xunit; | ||
|
||
public class MySqlXunitTestFrameworkExecutor : XunitTestFrameworkExecutor | ||
{ | ||
public MySqlXunitTestFrameworkExecutor( | ||
AssemblyName assemblyName, | ||
ISourceInformationProvider sourceInformationProvider, | ||
IMessageSink diagnosticMessageSink) | ||
: base(assemblyName, sourceInformationProvider, diagnosticMessageSink) | ||
{ | ||
} | ||
|
||
protected override async void RunTestCases( | ||
IEnumerable<IXunitTestCase> testCases, | ||
IMessageSink executionMessageSink, | ||
ITestFrameworkExecutionOptions executionOptions) | ||
{ | ||
using var assemblyRunner = new MySqlXunitTestAssemblyRunner( | ||
TestAssembly, | ||
testCases, | ||
DiagnosticMessageSink, | ||
executionMessageSink, | ||
executionOptions); | ||
await assemblyRunner.RunAsync(); | ||
} | ||
} |