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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
<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="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>
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;
25 changes: 19 additions & 6 deletions src/TestsOrder/TestPriorityOrderer.cs
Original file line number Diff line number Diff line change
@@ -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<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases)
public IReadOnlyCollection<TTestCase> OrderTestCases<TTestCase>(IReadOnlyCollection<TTestCase> testCases)
where TTestCase : ITestCase
{
var sortedMethods = new SortedDictionary<int, List<TTestCase>>();
Expand All @@ -11,22 +14,32 @@ public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> 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<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
3 changes: 1 addition & 2 deletions src/Xunit.Microsoft.DependencyInjection.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
<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="xunit.v3.extensibility.core" Version="2.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.5" />
</ItemGroup>
<ItemGroup>
Expand Down