Skip to content

Commit 1cb73a8

Browse files
committed
Add xunit logging to integration test
1 parent 17de1ba commit 1cb73a8

File tree

7 files changed

+53
-21
lines changed

7 files changed

+53
-21
lines changed

tests/Directory.Packages.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
55
</PropertyGroup>
66
<ItemGroup>
7+
<PackageVersion Include="MartinCostello.Logging.XUnit.v3" Version="0.6.0" />
78
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.9" />
89
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
910
<PackageVersion Include="Microsoft.Testing.Extensions.CodeCoverage" Version="18.0.4" />
@@ -14,4 +15,4 @@
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
<ProjectReference Include="..\..\src\Api\Api.csproj" />
1010
</ItemGroup>
1111
<ItemGroup>
12+
<PackageReference Include="MartinCostello.Logging.XUnit.v3" />
1213
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" />
1314
<PackageReference Include="Microsoft.NET.Test.Sdk" />
1415
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" />

tests/IntegrationTests/Setup/ApplicationFactory.cs

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

tests/IntegrationTests/Setup/IntegrationTestBase.cs

Lines changed: 5 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.SetOutputHelper(outputHelper);
1314
_client = _factory.CreateClient();
1415
}
1516

@@ -21,6 +22,7 @@ public virtual ValueTask InitializeAsync()
2122
public virtual ValueTask DisposeAsync()
2223
{
2324
_client.Dispose();
25+
_factory.ClearOutputHelper();
2426
_factory.Dispose();
2527
GC.SuppressFinalize(this);
2628
return ValueTask.CompletedTask;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Common;
2+
using MartinCostello.Logging.XUnit;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.Mvc.Testing;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.Logging;
7+
using Xunit;
8+
9+
namespace IntegrationTests.Setup;
10+
11+
public class TestApplicationFactory(Fixture fixture) : WebApplicationFactory<Program>, ITestOutputHelperAccessor
12+
{
13+
public ITestOutputHelper? OutputHelper { get; set; }
14+
15+
public virtual void ClearOutputHelper()
16+
{
17+
OutputHelper = null;
18+
}
19+
20+
public virtual void SetOutputHelper(ITestOutputHelper value)
21+
{
22+
OutputHelper = value;
23+
}
24+
25+
protected override void ConfigureWebHost(IWebHostBuilder builder)
26+
{
27+
if (EnvVarAccessors.UseMocking)
28+
{
29+
builder?.UseSetting(EnvVarKeys.MarketClientUrl, $"http://localhost:{fixture.GetPort()}");
30+
}
31+
32+
builder?.ConfigureLogging(x => x.ClearProviders().AddXUnit().SetMinimumLevel(LogLevel.Debug));
33+
}
34+
}

tests/IntegrationTests/packages.lock.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
"version": 2,
33
"dependencies": {
44
"net9.0": {
5+
"MartinCostello.Logging.XUnit.v3": {
6+
"type": "Direct",
7+
"requested": "[0.6.0, )",
8+
"resolved": "0.6.0",
9+
"contentHash": "9AjzwkmqezSeoJ+JmZRWUQsbYBF51RqYGIkTkCGj8PsdCVAOaRnsslyGRT9sQWiXsnDVvWqqCr0uKFNL0CtaPw==",
10+
"dependencies": {
11+
"Microsoft.Extensions.Logging": "8.0.1",
12+
"xunit.v3.extensibility.core": "2.0.1"
13+
}
14+
},
515
"Microsoft.AspNetCore.Mvc.Testing": {
616
"type": "Direct",
717
"requested": "[9.0.9, )",

0 commit comments

Comments
 (0)