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