Skip to content

Commit 74487dc

Browse files
committed
Create GremlinqTestFramework to filter tests early based on traits. The SideEffectTestCaseOrderer filtering can go. Only link AssemblyInfo.cs in .Tests projects.
1 parent b0e4519 commit 74487dc

File tree

6 files changed

+53
-16
lines changed

6 files changed

+53
-16
lines changed

test/AssemblyInfo.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
[assembly: CollectionBehavior(DisableTestParallelization = true)]
1+
using ExRam.Gremlinq.Tests.Infrastructure;
2+
3+
[assembly: CollectionBehavior(DisableTestParallelization = true)]
4+
[assembly: TestFramework(typeof(GremlinqTestFramework))]

test/Core.AspNet.Tests/ExRam.Gremlinq.Core.AspNet.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
<ItemGroup>
1313
<ProjectReference Include="..\..\src\Core.AspNet\ExRam.Gremlinq.Core.AspNet.csproj" />
14+
<ProjectReference Include="..\Tests.Infrastructure\ExRam.Gremlinq.Tests.Infrastructure.csproj" />
1415
</ItemGroup>
1516

1617
</Project>

test/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<TargetFrameworks Condition="'$(Configuration)' == 'Release'">net6.0;net7.0;net8.0;$(TargetFrameworks)</TargetFrameworks>
77
</PropertyGroup>
88

9-
<ItemGroup>
9+
<ItemGroup Condition="$(ProjectName.EndsWith('Tests'))">
1010
<Compile Include="$(MSBuildThisFileDirectory)AssemblyInfo.cs" Link="AssemblyInfo.cs" />
1111
</ItemGroup>
1212

test/Templates.Tests/ExRam.Gremlinq.Templates.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<ItemGroup>
1010
<PackageReference Include="Boxed.DotnetNewTest" />
1111
<PackageReference Include="System.Text.Json" />
12+
13+
<ProjectReference Include="..\Tests.Infrastructure\ExRam.Gremlinq.Tests.Infrastructure.csproj" />
1214
</ItemGroup>
1315

1416
</Project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Reflection;
2+
3+
using Xunit.Sdk;
4+
using Xunit.v3;
5+
6+
[assembly: CollectionBehavior(DisableTestParallelization = true)]
7+
8+
namespace ExRam.Gremlinq.Tests.Infrastructure
9+
{
10+
public sealed class GremlinqTestFramework : XunitTestFramework
11+
{
12+
private sealed class GremlinqTestFrameworkExecutor : ITestFrameworkExecutor
13+
{
14+
private readonly ITestFrameworkExecutor _baseExecutor;
15+
16+
public GremlinqTestFrameworkExecutor(ITestFrameworkExecutor baseExecutor)
17+
{
18+
_baseExecutor = baseExecutor;
19+
}
20+
21+
public ValueTask RunTestCases(IReadOnlyCollection<ITestCase> testCases, IMessageSink executionMessageSink, ITestFrameworkExecutionOptions executionOptions) => _baseExecutor.RunTestCases(
22+
testCases
23+
.Where(testCase =>
24+
{
25+
if (testCase.Traits.TryGetValue("Category", out var categories) && categories.Contains("IntegrationTest"))
26+
return testCase.Traits.TryGetValue("ValidPlatform", out var validPlatforms) && validPlatforms.Any(validPlatform => OperatingSystem.IsOSPlatform(validPlatform));
27+
28+
return true;
29+
})
30+
.ToArray(),
31+
executionMessageSink,
32+
executionOptions);
33+
}
34+
35+
public override string TestFrameworkDisplayName => "hallo";
36+
37+
protected override ITestFrameworkDiscoverer CreateDiscoverer(Assembly assembly) => base.CreateDiscoverer(assembly);
38+
39+
protected override ITestFrameworkExecutor CreateExecutor(Assembly assembly) => new GremlinqTestFrameworkExecutor(base.CreateExecutor(assembly));
40+
}
41+
}

test/Tests.Infrastructure/SideEffectTestCaseOrderer.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,9 @@ private static int GetIndex(string str) => str.StartsWith("Drop")
2323
: 2;
2424
}
2525

26-
public IReadOnlyCollection<TTestCase> OrderTestCases<TTestCase>(IReadOnlyCollection<TTestCase> testCases) where TTestCase : notnull, ITestCase
27-
{
28-
return testCases
29-
.Where(testCase =>
30-
{
31-
if (testCase.Traits.TryGetValue("Category", out var categories) && categories.Contains("IntegrationTest"))
32-
return testCase.Traits.TryGetValue("ValidPlatform", out var validPlatforms) && validPlatforms.Any(validPlatform => OperatingSystem.IsOSPlatform(validPlatform));
33-
34-
return true;
35-
})
36-
.OrderBy(x => x, TestCaseComparer<TTestCase>.Instance)
37-
.ThenBy(x => x!.TestMethod!.MethodName, StringComparer.OrdinalIgnoreCase)
38-
.ToArray();
39-
}
26+
public IReadOnlyCollection<TTestCase> OrderTestCases<TTestCase>(IReadOnlyCollection<TTestCase> testCases) where TTestCase : notnull, ITestCase => testCases
27+
.OrderBy(x => x, TestCaseComparer<TTestCase>.Instance)
28+
.ThenBy(x => x!.TestMethod!.MethodName, StringComparer.OrdinalIgnoreCase)
29+
.ToArray();
4030
}
4131
}

0 commit comments

Comments
 (0)