Skip to content

Update to xunit 3 #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion azure-pipeline-PR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
variables:
Major: 9
Minor: 0
Revision: 5
Revision: 6
BuildConfiguration: Release

name: $(Major).$(Minor).$(Revision)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -14,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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>59bdc82c-5628-47c8-a5ec-3630c3a2bc45</UserSecretsId>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.0">
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="xunit.v3" Version="2.0.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.6" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.6" />
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.6" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.6" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="9.0.6" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.6" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.6" />
</ItemGroup>
<ItemGroup>
<None Remove="appsettings.json" />
Expand Down
1 change: 0 additions & 1 deletion src/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -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;
29 changes: 22 additions & 7 deletions src/TestsOrder/TestPriorityOrderer.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
namespace Xunit.Microsoft.DependencyInjection.TestsOrder;
using System.Reflection;
using Xunit.v3;

namespace Xunit.Microsoft.DependencyInjection.TestsOrder;

public class TestPriorityOrderer : ITestCaseOrderer
{
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases)
public IReadOnlyCollection<TTestCase> OrderTestCases<TTestCase>(IReadOnlyCollection<TTestCase> testCases)
where TTestCase : ITestCase
{
var sortedMethods = new SortedDictionary<int, List<TTestCase>>();

foreach (var testCase in testCases)
{
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 ?? 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!)
{
priority = attr.GetNamedArgument<int>("Priority");
if (attr is TestOrderAttribute orderAttr)
{
priority = orderAttr.Priority;
}
}

GetOrCreate(sortedMethods, priority).Add(testCase);
}

var testCaseCollection = new List<TTestCase>();
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<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key)
Expand Down
13 changes: 6 additions & 7 deletions src/Xunit.Microsoft.DependencyInjection.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.5" />
<PackageReference Include="xunit.abstractions" Version="2.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.5" />
<PackageReference Include="xunit.core" Version="2.9.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.6" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.6" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="9.0.6" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.6" />
<PackageReference Include="xunit.v3.extensibility.core" Version="2.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.6" />
</ItemGroup>
<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\" />
Expand Down