Skip to content

Commit 9794c7b

Browse files
committed
Add serilog xunit sink for integration tests
1 parent 67e649b commit 9794c7b

File tree

8 files changed

+64
-25
lines changed

8 files changed

+64
-25
lines changed

Directory.Build.props

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
<NoWarn>S125;CA1848;CA1002;CA1515;S1118</NoWarn>
1919
</PropertyGroup>
2020
<PropertyGroup Condition="$(MSBuildProjectName.EndsWith('Tests'))">
21-
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
22-
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
23-
<TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>
24-
<TestingPlatformCaptureOutput>true</TestingPlatformCaptureOutput>
21+
<UseMicrosoftTestingPlatformRunner>false</UseMicrosoftTestingPlatformRunner>
22+
<TestingPlatformDotnetTestSupport>false</TestingPlatformDotnetTestSupport>
23+
<TestingPlatformShowTestsFailure>false</TestingPlatformShowTestsFailure>
24+
<TestingPlatformCaptureOutput>false</TestingPlatformCaptureOutput>
2525
<NoWarn>$(NoWarn);CA5394;CA1707</NoWarn>
2626
</PropertyGroup>
2727
<ItemGroup>

tests/Directory.Packages.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
99
<PackageVersion Include="Microsoft.Testing.Extensions.CodeCoverage" Version="18.0.4" />
1010
<PackageVersion Include="NSubstitute" Version="5.3.0" />
11+
<PackageVersion Include="Serilog.Sinks.XUnit3" Version="1.1.0" />
1112
<PackageVersion Include="Shouldly" Version="4.3.0" />
1213
<PackageVersion Include="SonarAnalyzer.CSharp" Version="10.15.0.120848" />
1314
<PackageVersion Include="Testcontainers" Version="4.7.0" />
1415
<PackageVersion Include="xunit.v3" Version="3.1.0" />
1516
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5" />
1617
</ItemGroup>
17-
</Project>
18+
</Project>

tests/IntegrationTests/ApiEndpointsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace IntegrationTests;
1111

12-
public sealed class ApiEndpointsTests(Fixture fixture) : IntegrationTestBase(fixture)
12+
public sealed class ApiEndpointsTests(Fixture fixture, ITestOutputHelper outputHelper) : IntegrationTestBase(fixture, outputHelper)
1313
{
1414
private const string DateFormat = "yyyy-MM-dd";
1515
private const string BaseUrl = "/api/v1";

tests/IntegrationTests/IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" />
1010
<PackageReference Include="Microsoft.NET.Test.Sdk" />
1111
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" />
12+
<PackageReference Include="Serilog.Sinks.XUnit3" />
1213
<PackageReference Include="Shouldly" />
1314
<PackageReference Include="Testcontainers" />
1415
<PackageReference Include="xunit.v3" />

tests/IntegrationTests/Setup/ApplicationFactory.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/IntegrationTests/Setup/IntegrationTestBase.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ namespace IntegrationTests.Setup;
44

55
public abstract class IntegrationTestBase : IAsyncLifetime
66
{
7-
internal readonly ApplicationFactory _factory;
7+
internal readonly TestApplicationFactory _factory;
88
internal readonly HttpClient _client;
99

10-
protected IntegrationTestBase(Fixture fixture)
10+
protected IntegrationTestBase(Fixture fixture, ITestOutputHelper outputHelper)
1111
{
12-
_factory = new ApplicationFactory(fixture);
12+
_factory = new TestApplicationFactory(fixture);
13+
_factory.SetTestOutputHelper(outputHelper);
1314
_client = _factory.CreateClient();
1415
}
1516

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Common;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.AspNetCore.Mvc.Testing;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Hosting;
6+
using Microsoft.Extensions.Options;
7+
using Serilog;
8+
using Serilog.Sinks.XUnit3;
9+
using Xunit;
10+
11+
namespace IntegrationTests.Setup;
12+
13+
public sealed class TestApplicationFactory(Fixture fixture) : WebApplicationFactory<Program>
14+
{
15+
protected override void ConfigureWebHost(IWebHostBuilder builder)
16+
{
17+
if (EnvVarAccessors.UseMocking)
18+
{
19+
builder?.UseSetting(EnvVarKeys.MarketClientUrl, $"http://localhost:{fixture.GetPort()}");
20+
}
21+
}
22+
23+
protected override IHost CreateHost(IHostBuilder builder)
24+
{
25+
builder.ConfigureServices(services =>
26+
{
27+
services.AddSingleton(Options.Create(new XUnit3TestOutputSinkOptions()));
28+
services.AddSingleton<XUnit3TestOutputSink>();
29+
});
30+
31+
builder.UseSerilog((ctx, sp, config) =>
32+
config.MinimumLevel.Information()
33+
.WriteTo.XUnit3TestOutput(sp.GetRequiredService<XUnit3TestOutputSink>())
34+
);
35+
36+
return base.CreateHost(builder);
37+
}
38+
39+
public void SetTestOutputHelper(ITestOutputHelper testOutputHelper) =>
40+
Services.GetRequiredService<XUnit3TestOutputSink>().TestOutputHelper = testOutputHelper;
41+
}

tests/IntegrationTests/packages.lock.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@
3434
"Microsoft.Testing.Platform": "1.8.4"
3535
}
3636
},
37+
"Serilog.Sinks.XUnit3": {
38+
"type": "Direct",
39+
"requested": "[1.1.0, )",
40+
"resolved": "1.1.0",
41+
"contentHash": "XqGy76yqRh9pj4FGqpjDlU8fjuw7QnXrYOuBNaLgtyvBn+/ALtCQqyiG6AqYByHkynCzkz1Dx383P1OHyyTg/Q==",
42+
"dependencies": {
43+
"Microsoft.Extensions.Options": "9.0.9",
44+
"Serilog": "4.3.0",
45+
"xunit.v3.extensibility.core": "3.0.1"
46+
}
47+
},
3748
"Shouldly": {
3849
"type": "Direct",
3950
"requested": "[4.3.0, )",

0 commit comments

Comments
 (0)