From 0f1aa4f3f71a8445c5d48088bdf8b744457a6868 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20D=C3=B6rnen?= Date: Mon, 16 Jun 2025 22:58:04 +0200 Subject: [PATCH 1/2] Update to xunit 3 --- .../GlobalUsings.cs | 1 - .../UnitTests.cs | 4 ++- ...ft.DependencyInjection.ExampleTests.csproj | 5 ++-- src/GlobalUsings.cs | 1 - src/TestsOrder/TestPriorityOrderer.cs | 25 ++++++++++++++----- ...Xunit.Microsoft.DependencyInjection.csproj | 3 +-- 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/GlobalUsings.cs b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/GlobalUsings.cs index 88048b1..0386e15 100644 --- a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/GlobalUsings.cs +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/GlobalUsings.cs @@ -2,7 +2,6 @@ global using Microsoft.Extensions.DependencyInjection; global using System.Collections.Generic; global using System.Threading.Tasks; -global using Xunit.Abstractions; global using Xunit.Microsoft.DependencyInjection.Abstracts; global using Xunit.Microsoft.DependencyInjection.Attributes; global using Xunit.Microsoft.DependencyInjection.ExampleTests.Fixtures; diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/UnitTests.cs b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/UnitTests.cs index e6bd000..1b7256f 100644 --- a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/UnitTests.cs +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/UnitTests.cs @@ -1,6 +1,8 @@ +using Xunit.Microsoft.DependencyInjection.TestsOrder; + namespace Xunit.Microsoft.DependencyInjection.ExampleTests; -[TestCaseOrderer("Xunit.Microsoft.DependencyInjection.TestsOrder.TestPriorityOrderer", "Xunit.Microsoft.DependencyInjection")] +[TestCaseOrderer(typeof(TestPriorityOrderer))] public class UnitTests { [Fact, TestOrder(1)] diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Xunit.Microsoft.DependencyInjection.ExampleTests.csproj b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Xunit.Microsoft.DependencyInjection.ExampleTests.csproj index 9097b30..bf30d47 100644 --- a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Xunit.Microsoft.DependencyInjection.ExampleTests.csproj +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Xunit.Microsoft.DependencyInjection.ExampleTests.csproj @@ -7,13 +7,14 @@ enable enable 59bdc82c-5628-47c8-a5ec-3630c3a2bc45 + Exe - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/GlobalUsings.cs b/src/GlobalUsings.cs index 6c0ebd4..22f9f4b 100644 --- a/src/GlobalUsings.cs +++ b/src/GlobalUsings.cs @@ -1,7 +1,6 @@ global using Microsoft.Extensions.Configuration; global using Microsoft.Extensions.DependencyInjection; global using Microsoft.Extensions.Logging; -global using Xunit.Abstractions; global using Xunit.Microsoft.DependencyInjection.Attributes; global using Xunit.Microsoft.DependencyInjection.Logging; global using Xunit.Sdk; \ No newline at end of file diff --git a/src/TestsOrder/TestPriorityOrderer.cs b/src/TestsOrder/TestPriorityOrderer.cs index 40f3036..1ee6734 100644 --- a/src/TestsOrder/TestPriorityOrderer.cs +++ b/src/TestsOrder/TestPriorityOrderer.cs @@ -1,8 +1,11 @@ -namespace Xunit.Microsoft.DependencyInjection.TestsOrder; +using System.Reflection; +using Xunit.v3; + +namespace Xunit.Microsoft.DependencyInjection.TestsOrder; public class TestPriorityOrderer : ITestCaseOrderer { - public IEnumerable OrderTestCases(IEnumerable testCases) + public IReadOnlyCollection OrderTestCases(IReadOnlyCollection testCases) where TTestCase : ITestCase { var sortedMethods = new SortedDictionary>(); @@ -11,22 +14,32 @@ public IEnumerable OrderTestCases(IEnumerable t { var priority = 0; - foreach (var attr in testCase.TestMethod.Method.GetCustomAttributes(typeof(TestOrderAttribute).AssemblyQualifiedName)) + var testMethod = testCase.TestMethod; + var type = Type.GetType(testMethod.TestClass.TestClassNamespace); + var method = type?.GetMethod(testMethod.MethodName); + var attributes = method?.GetCustomAttributes(typeof(TestOrderAttribute)); + foreach (var attr in attributes) { - priority = attr.GetNamedArgument("Priority"); + if (attr is TestOrderAttribute orderAttr) + { + priority = orderAttr.Priority; + } } GetOrCreate(sortedMethods, priority).Add(testCase); } + var testCaseCollection = new List(); foreach (var list in sortedMethods.Keys.Select(priority => sortedMethods[priority])) { - list.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.Method.Name, y.TestMethod.Method.Name)); + list.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.MethodName, y.TestMethod.MethodName)); foreach (var testCase in list) { - yield return testCase; + testCaseCollection.Add(testCase); } } + + return testCaseCollection.AsReadOnly(); } private TValue GetOrCreate(IDictionary dictionary, TKey key) diff --git a/src/Xunit.Microsoft.DependencyInjection.csproj b/src/Xunit.Microsoft.DependencyInjection.csproj index 5a18242..c0ebc0d 100644 --- a/src/Xunit.Microsoft.DependencyInjection.csproj +++ b/src/Xunit.Microsoft.DependencyInjection.csproj @@ -9,10 +9,9 @@ - - + From f6edbe02fb9642c72dbaae66635bf943143affa2 Mon Sep 17 00:00:00 2001 From: Arash Sabet Date: Sun, 6 Jul 2025 09:56:03 -0400 Subject: [PATCH 2/2] Update dependencies and improve test ordering logic #298 Bump Microsoft.Extensions.* and Microsoft.NET.Test.Sdk package versions to latest patch releases in both main and example projects. Update Azure Pipelines to use .NET SDK 9.0.301 and increment revision. Improve null safety and type resolution in TestPriorityOrderer, and update example tests to use cancellation tokens with Task.Delay. --- azure-pipeline-PR.yml | 2 +- azure-pipelines.yml | 4 ++-- .../UnitTests.cs | 4 ++-- ...oft.DependencyInjection.ExampleTests.csproj | 18 +++++++++--------- src/TestsOrder/TestPriorityOrderer.cs | 12 +++++++----- src/Xunit.Microsoft.DependencyInjection.csproj | 10 +++++----- 6 files changed, 26 insertions(+), 24 deletions(-) diff --git a/azure-pipeline-PR.yml b/azure-pipeline-PR.yml index db08c7e..a7634c9 100644 --- a/azure-pipeline-PR.yml +++ b/azure-pipeline-PR.yml @@ -17,7 +17,7 @@ steps: displayName: 'Use .NET 9.0 sdk' inputs: packageType: sdk - version: 9.0.300 + version: 9.0.301 installationPath: $(Agent.ToolsDirectory)/dotnet - script: echo Started restoring the source code - task: DotNetCoreCLI@2 diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 99e34ba..d2caaa1 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -1,7 +1,7 @@ variables: Major: 9 Minor: 0 - Revision: 5 + Revision: 6 BuildConfiguration: Release name: $(Major).$(Minor).$(Revision) @@ -25,7 +25,7 @@ steps: displayName: 'Use .NET 9.0 sdk' inputs: packageType: sdk - version: 9.0.300 + version: 9.0.301 installationPath: $(Agent.ToolsDirectory)/dotnet - script: echo Started restoring the source code - task: DotNetCoreCLI@2 diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/UnitTests.cs b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/UnitTests.cs index 1b7256f..e58545f 100644 --- a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/UnitTests.cs +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/UnitTests.cs @@ -16,14 +16,14 @@ public void Test2() [Fact, TestOrder(3)] public async Task Test3() { - await Task.Delay(3000); + await Task.Delay(3000, TestContext.Current.CancellationToken); Assert.Equal(1, 1); } [Fact, TestOrder(4)] public async Task Test4() { - await Task.Delay(5000); + await Task.Delay(5000, TestContext.Current.CancellationToken); Assert.True(1 > 0); } } diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Xunit.Microsoft.DependencyInjection.ExampleTests.csproj b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Xunit.Microsoft.DependencyInjection.ExampleTests.csproj index bf30d47..a16e5c0 100644 --- a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Xunit.Microsoft.DependencyInjection.ExampleTests.csproj +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Xunit.Microsoft.DependencyInjection.ExampleTests.csproj @@ -11,8 +11,8 @@ - - + + all @@ -22,13 +22,13 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - + + + + + + + diff --git a/src/TestsOrder/TestPriorityOrderer.cs b/src/TestsOrder/TestPriorityOrderer.cs index 1ee6734..6b3bdaf 100644 --- a/src/TestsOrder/TestPriorityOrderer.cs +++ b/src/TestsOrder/TestPriorityOrderer.cs @@ -13,12 +13,14 @@ public IReadOnlyCollection OrderTestCases(IReadOnlyCollect foreach (var testCase in testCases) { var priority = 0; - var testMethod = testCase.TestMethod; - var type = Type.GetType(testMethod.TestClass.TestClassNamespace); - var method = type?.GetMethod(testMethod.MethodName); + var type = Type.GetType(testMethod?.TestClass.TestClassNamespace ?? string.Empty) ?? AppDomain.CurrentDomain + .GetAssemblies() + .Select(a => a.GetType(testMethod?.TestClass?.TestClassName ?? string.Empty)) + .FirstOrDefault(t => t != null); + var method = type?.GetMethod(testMethod?.MethodName ?? string.Empty); var attributes = method?.GetCustomAttributes(typeof(TestOrderAttribute)); - foreach (var attr in attributes) + foreach (var attr in attributes!) { if (attr is TestOrderAttribute orderAttr) { @@ -32,7 +34,7 @@ public IReadOnlyCollection OrderTestCases(IReadOnlyCollect var testCaseCollection = new List(); foreach (var list in sortedMethods.Keys.Select(priority => sortedMethods[priority])) { - list.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.MethodName, y.TestMethod.MethodName)); + list.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod?.MethodName, y.TestMethod?.MethodName)); foreach (var testCase in list) { testCaseCollection.Add(testCase); diff --git a/src/Xunit.Microsoft.DependencyInjection.csproj b/src/Xunit.Microsoft.DependencyInjection.csproj index c0ebc0d..51261e8 100644 --- a/src/Xunit.Microsoft.DependencyInjection.csproj +++ b/src/Xunit.Microsoft.DependencyInjection.csproj @@ -7,12 +7,12 @@ README.md - - - - + + + + - +