using Xunit.Abstractions; using Xunit.Sdk; namespace SqsEventHandler.IntegrationTests.Utilities; public class PriorityOrderer : ITestCaseOrderer { public IEnumerable OrderTestCases( IEnumerable testCases) where TTestCase : ITestCase { var assemblyName = typeof(TestPriorityAttribute).AssemblyQualifiedName!; var sortedMethods = new SortedDictionary>(); foreach (var testCase in testCases) { var priority = testCase.TestMethod.Method .GetCustomAttributes(assemblyName) .FirstOrDefault() ?.GetNamedArgument(nameof(TestPriorityAttribute.Priority)) ?? 0; GetOrCreate(sortedMethods, priority).Add(testCase); } foreach (var testCase in sortedMethods.Keys.SelectMany( priority => sortedMethods[priority].OrderBy( testCase => testCase.TestMethod.Method.Name))) { yield return testCase; } } private static TValue GetOrCreate( IDictionary dictionary, TKey key) where TKey : struct where TValue : new() => dictionary.TryGetValue(key, out var result) ? result : (dictionary[key] = new TValue()); }