Skip to content

Commit 0f1aa4f

Browse files
committed
Update to xunit 3
1 parent a1fa952 commit 0f1aa4f

File tree

6 files changed

+26
-13
lines changed

6 files changed

+26
-13
lines changed

examples/Xunit.Microsoft.DependencyInjection.ExampleTests/GlobalUsings.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
global using Microsoft.Extensions.DependencyInjection;
33
global using System.Collections.Generic;
44
global using System.Threading.Tasks;
5-
global using Xunit.Abstractions;
65
global using Xunit.Microsoft.DependencyInjection.Abstracts;
76
global using Xunit.Microsoft.DependencyInjection.Attributes;
87
global using Xunit.Microsoft.DependencyInjection.ExampleTests.Fixtures;

examples/Xunit.Microsoft.DependencyInjection.ExampleTests/UnitTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using Xunit.Microsoft.DependencyInjection.TestsOrder;
2+
13
namespace Xunit.Microsoft.DependencyInjection.ExampleTests;
24

3-
[TestCaseOrderer("Xunit.Microsoft.DependencyInjection.TestsOrder.TestPriorityOrderer", "Xunit.Microsoft.DependencyInjection")]
5+
[TestCaseOrderer(typeof(TestPriorityOrderer))]
46
public class UnitTests
57
{
68
[Fact, TestOrder(1)]

examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Xunit.Microsoft.DependencyInjection.ExampleTests.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
<Nullable>enable</Nullable>
88
<ImplicitUsings>enable</ImplicitUsings>
99
<UserSecretsId>59bdc82c-5628-47c8-a5ec-3630c3a2bc45</UserSecretsId>
10+
<OutputType>Exe</OutputType>
1011
</PropertyGroup>
1112

1213
<ItemGroup>
1314
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.5" />
1415
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
15-
<PackageReference Include="xunit" Version="2.9.3" />
16-
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.0">
16+
<PackageReference Include="xunit.v3" Version="2.0.3" />
17+
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.1">
1718
<PrivateAssets>all</PrivateAssets>
1819
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1920
</PackageReference>

src/GlobalUsings.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
global using Microsoft.Extensions.Configuration;
22
global using Microsoft.Extensions.DependencyInjection;
33
global using Microsoft.Extensions.Logging;
4-
global using Xunit.Abstractions;
54
global using Xunit.Microsoft.DependencyInjection.Attributes;
65
global using Xunit.Microsoft.DependencyInjection.Logging;
76
global using Xunit.Sdk;

src/TestsOrder/TestPriorityOrderer.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
namespace Xunit.Microsoft.DependencyInjection.TestsOrder;
1+
using System.Reflection;
2+
using Xunit.v3;
3+
4+
namespace Xunit.Microsoft.DependencyInjection.TestsOrder;
25

36
public class TestPriorityOrderer : ITestCaseOrderer
47
{
5-
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases)
8+
public IReadOnlyCollection<TTestCase> OrderTestCases<TTestCase>(IReadOnlyCollection<TTestCase> testCases)
69
where TTestCase : ITestCase
710
{
811
var sortedMethods = new SortedDictionary<int, List<TTestCase>>();
@@ -11,22 +14,32 @@ public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> t
1114
{
1215
var priority = 0;
1316

14-
foreach (var attr in testCase.TestMethod.Method.GetCustomAttributes(typeof(TestOrderAttribute).AssemblyQualifiedName))
17+
var testMethod = testCase.TestMethod;
18+
var type = Type.GetType(testMethod.TestClass.TestClassNamespace);
19+
var method = type?.GetMethod(testMethod.MethodName);
20+
var attributes = method?.GetCustomAttributes(typeof(TestOrderAttribute));
21+
foreach (var attr in attributes)
1522
{
16-
priority = attr.GetNamedArgument<int>("Priority");
23+
if (attr is TestOrderAttribute orderAttr)
24+
{
25+
priority = orderAttr.Priority;
26+
}
1727
}
1828

1929
GetOrCreate(sortedMethods, priority).Add(testCase);
2030
}
2131

32+
var testCaseCollection = new List<TTestCase>();
2233
foreach (var list in sortedMethods.Keys.Select(priority => sortedMethods[priority]))
2334
{
24-
list.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.Method.Name, y.TestMethod.Method.Name));
35+
list.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.MethodName, y.TestMethod.MethodName));
2536
foreach (var testCase in list)
2637
{
27-
yield return testCase;
38+
testCaseCollection.Add(testCase);
2839
}
2940
}
41+
42+
return testCaseCollection.AsReadOnly();
3043
}
3144

3245
private TValue GetOrCreate<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key)

src/Xunit.Microsoft.DependencyInjection.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
<ItemGroup>
1010
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.5" />
1111
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.5" />
12-
<PackageReference Include="xunit.abstractions" Version="2.0.3" />
1312
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="9.0.5" />
1413
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.5" />
15-
<PackageReference Include="xunit.core" Version="2.9.3" />
14+
<PackageReference Include="xunit.v3.extensibility.core" Version="2.0.3" />
1615
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.5" />
1716
</ItemGroup>
1817
<ItemGroup>

0 commit comments

Comments
 (0)