Skip to content

Commit 244250a

Browse files
committed
Output server side errors with stack trace in integration testing
1 parent 706f946 commit 244250a

File tree

6 files changed

+35
-2
lines changed

6 files changed

+35
-2
lines changed

App/Services/MarketService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public class MarketService(ILogger<MarketService> logger, IMarketClient marketCl
2121
var prices = data.Select(x => x.Price).ToList();
2222
var longestDownwardPriceTrend = prices.LongestConsecutiveDecreasingSubset();
2323
_logger.LogInformation("Longest downward price trend {LongestDownwardPriceTrend} days.", longestDownwardPriceTrend);
24-
return longestDownwardPriceTrend;
24+
throw new InvalidOperationException("TEST EXCEPTION");
25+
// return longestDownwardPriceTrend;
2526
}
2627

2728
public async Task<(DateOnly Date, decimal Volume)?> GetHighestTradingVolume(DateOnly fromDate, DateOnly toDate)

Tests/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
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.5.1" />
78
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.4" />
89
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
910
<PackageVersion Include="Microsoft.Testing.Extensions.CodeCoverage" Version="17.14.2" />

Tests/IntegrationTests/ApiEndpointsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public sealed class ApiEndpointsTests(ApplicationFactory factory) : IntegrationT
2424
[MemberData(nameof(Cases))]
2525
public async Task LongestDownwardTrend(string? fromDate, string? toDate, HttpStatusCode status)
2626
{
27+
_factory.SetOutputHelper(TestContext.Current.TestOutputHelper);
2728
var ct = TestContext.Current.CancellationToken;
2829
var result = await _client.GetAsync($"{BaseUrl}/longestdownwardtrend?fromDate={fromDate}&toDate={toDate}", cancellationToken: ct);
2930
result.StatusCode.ShouldBeOneOf(status, HttpStatusCode.TooManyRequests);

Tests/IntegrationTests/ApplicationFactory.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Logging;
13
using Microsoft.AspNetCore.Mvc.Testing;
24
using Xunit;
5+
using MartinCostello.Logging.XUnit;
36

47
namespace IntegrationTests;
58

6-
public sealed class ApplicationFactory : WebApplicationFactory<Program>, IAsyncLifetime
9+
public sealed class ApplicationFactory : WebApplicationFactory<Program>, IAsyncLifetime, ITestOutputHelperAccessor
710
{
11+
12+
public ITestOutputHelper? OutputHelper { get; set; }
13+
14+
public void ClearOutputHelper()
15+
=> OutputHelper = null;
16+
17+
public void SetOutputHelper(ITestOutputHelper? value)
18+
=> OutputHelper = value;
19+
20+
protected override void ConfigureWebHost(IWebHostBuilder builder)
21+
{
22+
builder.ConfigureLogging(loggingBuilder =>
23+
loggingBuilder.ClearProviders().AddXUnit(this)
24+
);
25+
}
26+
827
public ValueTask InitializeAsync()
928
{
1029
return ValueTask.CompletedTask;

Tests/IntegrationTests/IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88
<ItemGroup>
99
<ProjectReference Include="..\..\App\Api\Api.csproj" />
10+
<PackageReference Include="MartinCostello.Logging.XUnit.v3" />
1011
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" />
1112
<PackageReference Include="Microsoft.NET.Test.Sdk" />
1213
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" />

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.5.1, )",
8+
"resolved": "0.5.1",
9+
"contentHash": "VBh06CBetEzaVJbSajn78sn8HZMnL5ZS7RKL4IGbyYkrw65cq7AueKIUw/oYDxwzd/gw2myBuIUjib5EJnEWgg==",
10+
"dependencies": {
11+
"Microsoft.Extensions.Logging": "8.0.1",
12+
"xunit.v3.extensibility.core": "1.0.0"
13+
}
14+
},
515
"Microsoft.AspNetCore.Mvc.Testing": {
616
"type": "Direct",
717
"requested": "[9.0.4, )",

0 commit comments

Comments
 (0)