Skip to content

Commit

Permalink
Create GremlinqTestFramework to filter tests early based on traits. T…
Browse files Browse the repository at this point in the history
…he SideEffectTestCaseOrderer filtering can go. Only link AssemblyInfo.cs in .Tests projects.
  • Loading branch information
danielcweber committed Dec 18, 2024
1 parent b0e4519 commit 74487dc
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
5 changes: 4 additions & 1 deletion test/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
[assembly: CollectionBehavior(DisableTestParallelization = true)]
using ExRam.Gremlinq.Tests.Infrastructure;

[assembly: CollectionBehavior(DisableTestParallelization = true)]
[assembly: TestFramework(typeof(GremlinqTestFramework))]
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Core.AspNet\ExRam.Gremlinq.Core.AspNet.csproj" />
<ProjectReference Include="..\Tests.Infrastructure\ExRam.Gremlinq.Tests.Infrastructure.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion test/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<TargetFrameworks Condition="'$(Configuration)' == 'Release'">net6.0;net7.0;net8.0;$(TargetFrameworks)</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<ItemGroup Condition="$(ProjectName.EndsWith('Tests'))">
<Compile Include="$(MSBuildThisFileDirectory)AssemblyInfo.cs" Link="AssemblyInfo.cs" />
</ItemGroup>

Expand Down
2 changes: 2 additions & 0 deletions test/Templates.Tests/ExRam.Gremlinq.Templates.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<ItemGroup>
<PackageReference Include="Boxed.DotnetNewTest" />
<PackageReference Include="System.Text.Json" />

<ProjectReference Include="..\Tests.Infrastructure\ExRam.Gremlinq.Tests.Infrastructure.csproj" />
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions test/Tests.Infrastructure/GremlinqTestFramework.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Reflection;

using Xunit.Sdk;
using Xunit.v3;

[assembly: CollectionBehavior(DisableTestParallelization = true)]

namespace ExRam.Gremlinq.Tests.Infrastructure
{
public sealed class GremlinqTestFramework : XunitTestFramework
{
private sealed class GremlinqTestFrameworkExecutor : ITestFrameworkExecutor
{
private readonly ITestFrameworkExecutor _baseExecutor;

public GremlinqTestFrameworkExecutor(ITestFrameworkExecutor baseExecutor)
{
_baseExecutor = baseExecutor;
}

public ValueTask RunTestCases(IReadOnlyCollection<ITestCase> testCases, IMessageSink executionMessageSink, ITestFrameworkExecutionOptions executionOptions) => _baseExecutor.RunTestCases(
testCases
.Where(testCase =>
{
if (testCase.Traits.TryGetValue("Category", out var categories) && categories.Contains("IntegrationTest"))
return testCase.Traits.TryGetValue("ValidPlatform", out var validPlatforms) && validPlatforms.Any(validPlatform => OperatingSystem.IsOSPlatform(validPlatform));

return true;
})
.ToArray(),
executionMessageSink,
executionOptions);
}

public override string TestFrameworkDisplayName => "hallo";

protected override ITestFrameworkDiscoverer CreateDiscoverer(Assembly assembly) => base.CreateDiscoverer(assembly);

protected override ITestFrameworkExecutor CreateExecutor(Assembly assembly) => new GremlinqTestFrameworkExecutor(base.CreateExecutor(assembly));
}
}
18 changes: 4 additions & 14 deletions test/Tests.Infrastructure/SideEffectTestCaseOrderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,9 @@ private static int GetIndex(string str) => str.StartsWith("Drop")
: 2;
}

public IReadOnlyCollection<TTestCase> OrderTestCases<TTestCase>(IReadOnlyCollection<TTestCase> testCases) where TTestCase : notnull, ITestCase
{
return testCases
.Where(testCase =>
{
if (testCase.Traits.TryGetValue("Category", out var categories) && categories.Contains("IntegrationTest"))
return testCase.Traits.TryGetValue("ValidPlatform", out var validPlatforms) && validPlatforms.Any(validPlatform => OperatingSystem.IsOSPlatform(validPlatform));

return true;
})
.OrderBy(x => x, TestCaseComparer<TTestCase>.Instance)
.ThenBy(x => x!.TestMethod!.MethodName, StringComparer.OrdinalIgnoreCase)
.ToArray();
}
public IReadOnlyCollection<TTestCase> OrderTestCases<TTestCase>(IReadOnlyCollection<TTestCase> testCases) where TTestCase : notnull, ITestCase => testCases
.OrderBy(x => x, TestCaseComparer<TTestCase>.Instance)
.ThenBy(x => x!.TestMethod!.MethodName, StringComparer.OrdinalIgnoreCase)
.ToArray();
}
}

0 comments on commit 74487dc

Please sign in to comment.