Skip to content

Commit

Permalink
Migrate to xunit v3
Browse files Browse the repository at this point in the history
Migrate to xunit v3 for tests.
  • Loading branch information
martincostello committed Dec 20, 2024
1 parent af6480c commit 498b1cf
Show file tree
Hide file tree
Showing 18 changed files with 103 additions and 99 deletions.
2 changes: 0 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@
</ItemGroup>
<ItemGroup Condition=" '$(IsTestProject)' == 'true' ">
<Using Include="Shouldly" />
<Using Include="xRetry" />
<Using Include="Xunit" />
<Using Include="Xunit.Abstractions" />
</ItemGroup>
</Project>
6 changes: 2 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@
<PackageVersion Include="AWSSDK.SQS" Version="3.7.400.3" />
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" />
<PackageVersion Include="GitHubActionsTestLogger" Version="2.4.1" />
<PackageVersion Include="MartinCostello.Logging.XUnit" Version="0.4.0" />
<PackageVersion Include="MartinCostello.Logging.XUnit.v3" Version="0.5.0" />
<PackageVersion Include="Microsoft.AspNetCore.TestHost" Version="9.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="NSubstitute" Version="5.3.0" />
<PackageVersion Include="ReportGenerator" Version="5.4.1" />
<PackageVersion Include="Shouldly" Version="4.2.1" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xRetry" Version="1.9.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.0" />
<PackageVersion Include="Xunit.SkippableFact" Version="1.5.23" />
<PackageVersion Include="xunit.v3" Version="1.0.0" />
</ItemGroup>
<ItemGroup Condition=" '$(IsTestProject)' == 'true' ">
<PackageReference Include="coverlet.msbuild" PrivateAssets="All" />
Expand Down
4 changes: 2 additions & 2 deletions samples/MathsFunctions.Tests/MathsFunctions.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<NoWarn>$(NoWarn);CA1062;CA1707;CA2007;CA2234;SA1600</NoWarn>
<OutputType>Exe</OutputType>
<RootNamespace>MathsFunctions</RootNamespace>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>
Expand All @@ -12,9 +13,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Shouldly" />
<PackageReference Include="xRetry" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="xunit.v3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MathsFunctions\MathsFunctions.csproj" />
Expand Down
4 changes: 2 additions & 2 deletions samples/MinimalApi.Tests/ApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public ApiTests(ITestOutputHelper outputHelper)
public void Dispose()
=> _server.Dispose();

public async Task DisposeAsync()
public async ValueTask DisposeAsync()
=> await _server.DisposeAsync();

public async Task InitializeAsync()
public async ValueTask InitializeAsync()
=> await _server.InitializeAsync();

[Fact(Timeout = 5_000)]
Expand Down
4 changes: 2 additions & 2 deletions samples/MinimalApi.Tests/HttpLambdaTestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal sealed class HttpLambdaTestServer : LambdaTestServer, IAsyncLifetime, I

public ITestOutputHelper? OutputHelper { get; set; }

public async Task DisposeAsync()
public async ValueTask DisposeAsync()
{
if (_webHost is not null)
{
Expand All @@ -28,7 +28,7 @@ public async Task DisposeAsync()
Dispose();
}

public async Task InitializeAsync()
public async ValueTask InitializeAsync()
=> await StartAsync(_cts.Token);

protected override IServer CreateServer(WebHostBuilder builder)
Expand Down
6 changes: 3 additions & 3 deletions samples/MinimalApi.Tests/MinimalApi.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<NoWarn>$(NoWarn);CA1062;CA1707;CA1861;CA2007;CA2234;SA1600</NoWarn>
<OutputType>Exe</OutputType>
<RootNamespace>MinimalApi</RootNamespace>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MartinCostello.Logging.XUnit" />
<PackageReference Include="MartinCostello.Logging.XUnit.v3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Shouldly" />
<PackageReference Include="xRetry" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="xunit.v3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MinimalApi\MinimalApi.csproj" />
Expand Down
9 changes: 4 additions & 5 deletions tests/AwsLambdaTestServer.Tests/AssemblyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using System.Diagnostics;
using Xunit.Sdk;
using MartinCostello.Testing.AwsLambdaTestServer;

[assembly: TestFramework("MartinCostello.Testing.AwsLambdaTestServer.AssemblyFixture", "MartinCostello.Testing.AwsLambdaTestServer.Tests")]
[assembly: AssemblyFixture(typeof(AssemblyFixture))]

namespace MartinCostello.Testing.AwsLambdaTestServer;

public sealed class AssemblyFixture : XunitTestFramework
public sealed class AssemblyFixture
{
// Read the default memory limits before any of the tests execute any code that may change it.
// The cast to ulong is required for the setting to be respected by the runtime.
// See https://github.com/aws/aws-lambda-dotnet/pull/1595#issuecomment-1771747410.
private static readonly ulong DefaultMemory = (ulong)GC.GetGCMemoryInfo().TotalAvailableMemoryBytes;

public AssemblyFixture(IMessageSink messageSink)
: base(messageSink)
public AssemblyFixture()
{
ResetMemoryLimits();
}
Expand Down
22 changes: 12 additions & 10 deletions tests/AwsLambdaTestServer.Tests/AwsIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ namespace MartinCostello.Testing.AwsLambdaTestServer;

public static class AwsIntegrationTests
{
[SkippableFact]
[Fact]
public static async Task Runtime_Generates_Valid_Aws_Trace_Id()
{
// Arrange
Xunit.Skip.If(GetAwsCredentials() is null, "No AWS credentials are configured.");
Assert.SkipWhen(GetAwsCredentials() is null, "No AWS credentials are configured.");

using var server = new LambdaTestServer();
using var cancellationTokenSource = new CancellationTokenSource();
Expand All @@ -33,15 +33,17 @@ public static async Task Runtime_Generates_Valid_Aws_Trace_Id()
var json = System.Text.Json.JsonSerializer.Serialize(request);
var context = await server.EnqueueAsync(json);

_ = Task.Run(async () =>
{
await context.Response.WaitToReadAsync(cancellationTokenSource.Token);

if (!cancellationTokenSource.IsCancellationRequested)
_ = Task.Run(
async () =>
{
await cancellationTokenSource.CancelAsync();
}
});
await context.Response.WaitToReadAsync(cancellationTokenSource.Token);

if (!cancellationTokenSource.IsCancellationRequested)
{
await cancellationTokenSource.CancelAsync();
}
},
cancellationTokenSource.Token);

using var httpClient = server.CreateClient();

Expand Down
18 changes: 10 additions & 8 deletions tests/AwsLambdaTestServer.Tests/Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ public static async Task Function_Can_Process_Request()
LambdaTestContext context = await server.EnqueueAsync(requestJson);

// Queue a task to stop the test server from listening as soon as the response is available
_ = Task.Run(async () =>
{
await context.Response.WaitToReadAsync(cancellationTokenSource.Token);

if (!cancellationTokenSource.IsCancellationRequested)
_ = Task.Run(
async () =>
{
await cancellationTokenSource.CancelAsync();
}
});
await context.Response.WaitToReadAsync(cancellationTokenSource.Token);

if (!cancellationTokenSource.IsCancellationRequested)
{
await cancellationTokenSource.CancelAsync();
}
},
cancellationTokenSource.Token);

// Create an HttpClient for the Lambda to use with LambdaBootstrap
using var httpClient = server.CreateClient();
Expand Down
4 changes: 2 additions & 2 deletions tests/AwsLambdaTestServer.Tests/HttpLambdaTestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public HttpLambdaTestServer(Action<IServiceCollection> configure)

public ITestOutputHelper? OutputHelper { get; set; }

public async Task DisposeAsync()
public async ValueTask DisposeAsync()
{
if (_webHost is not null)
{
Expand All @@ -37,7 +37,7 @@ public async Task DisposeAsync()
Dispose();
}

public async Task InitializeAsync()
public async ValueTask InitializeAsync()
{
Options.Configure = (services) =>
services.AddLogging((builder) => builder.AddXUnit(this));
Expand Down
62 changes: 34 additions & 28 deletions tests/AwsLambdaTestServer.Tests/HttpLambdaTestServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace MartinCostello.Testing.AwsLambdaTestServer;

#pragma warning disable JSON002

[Collection(nameof(LambdaTestServerCollection))]
[Collection<LambdaTestServerCollection>]
public class HttpLambdaTestServerTests(ITestOutputHelper outputHelper) : ITestOutputHelperAccessor
{
public ITestOutputHelper? OutputHelper { get; set; } = outputHelper;
Expand All @@ -31,15 +31,17 @@ void Configure(IServiceCollection services)

var context = await server.EnqueueAsync("""{"Values": [ 1, 2, 3 ]}""");

_ = Task.Run(async () =>
{
await context.Response.WaitToReadAsync(cts.Token);

if (!cts.IsCancellationRequested)
_ = Task.Run(
async () =>
{
await cts.CancelAsync();
}
});
await context.Response.WaitToReadAsync(cts.Token);

if (!cts.IsCancellationRequested)
{
await cts.CancelAsync();
}
},
cts.Token);

using var httpClient = server.CreateClient();

Expand Down Expand Up @@ -72,15 +74,17 @@ void Configure(IServiceCollection services)

var context = await server.EnqueueAsync("""{"Values": null}""");

_ = Task.Run(async () =>
{
await context.Response.WaitToReadAsync(cts.Token);

if (!cts.IsCancellationRequested)
_ = Task.Run(
async () =>
{
await cts.CancelAsync();
}
});
await context.Response.WaitToReadAsync(cts.Token);

if (!cts.IsCancellationRequested)
{
await cts.CancelAsync();
}
},
cts.Token);

using var httpClient = server.CreateClient();

Expand Down Expand Up @@ -121,18 +125,20 @@ void Configure(IServiceCollection services)
channels.Add((request.Values.Sum(), await server.EnqueueAsync(request)));
}

_ = Task.Run(async () =>
{
foreach ((var _, var context) in channels)
{
await context.Response.WaitToReadAsync(cts.Token);
}

if (!cts.IsCancellationRequested)
_ = Task.Run(
async () =>
{
await cts.CancelAsync();
}
});
foreach ((var _, var context) in channels)
{
await context.Response.WaitToReadAsync(cts.Token);
}

if (!cts.IsCancellationRequested)
{
await cts.CancelAsync();
}
},
cts.Token);

using var httpClient = server.CreateClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@
namespace MartinCostello.Testing.AwsLambdaTestServer;

[CollectionDefinition(nameof(LambdaTestServerCollection), DisableParallelization = true)]
public static class LambdaTestServerCollection
{
}
public class LambdaTestServerCollection;
Loading

0 comments on commit 498b1cf

Please sign in to comment.