ConnectingApps.Xunit.TestLogger
1.1.0
See the version list below for details.
dotnet add package ConnectingApps.Xunit.TestLogger --version 1.1.0
NuGet\Install-Package ConnectingApps.Xunit.TestLogger -Version 1.1.0
<PackageReference Include="ConnectingApps.Xunit.TestLogger" Version="1.1.0" />
paket add ConnectingApps.Xunit.TestLogger --version 1.1.0
#r "nuget: ConnectingApps.Xunit.TestLogger, 1.1.0"
// Install ConnectingApps.Xunit.TestLogger as a Cake Addin
#addin nuget:?package=ConnectingApps.Xunit.TestLogger&version=1.1.0
// Install ConnectingApps.Xunit.TestLogger as a Cake Tool
#tool nuget:?package=ConnectingApps.Xunit.TestLogger&version=1.1.0
Xunit.TestLogger
Before this NuGet package
If you make integration tests with xUnit, your logging is gone. You can just see if the test succeeds or fails but you cannot see why as you don't have the logging to help you understanding what happened during the runtime of the test, which makes it hard to solve it.
Now
The is situation is different now! The problem is solved for .NET 6, .NET 7 and .NET 8. Alle you need to do is stop using this way of creating a WebApplicationFactory
:
public NoLoggingTest()
{
_factory = new WebApplicationFactory<Program>();
}
and start creating it this way
public ImprovedLoggingTest(ITestOutputHelper output)
{
_factory = new TestLoggerWebApplicationFactory<Program>(output);
}
Here is how it looks like:
In Detail
Assume you have the following controller method (just the method from the template in Visual Studio with some logging added):
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
// first do some logging
_logger.LogInformation("This should be logged during testing");
// Then return an object like it is done in the VS Template
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
then you can test it like this:
using ConnectingApps.Xunit.TestLogger;
using System.Net;
using Xunit.Abstractions;
namespace ConnectingApps.LoggingWebApi.IntegrationTest
{
public class ImprovedLoggingTest : IDisposable
{
private readonly TestLoggerWebApplicationFactory<Program> _factory;
private readonly HttpClient _client;
public ImprovedLoggingTest(ITestOutputHelper output)
{
_factory = new TestLoggerWebApplicationFactory<Program>(output);
_client = _factory.CreateClient();
}
[Fact]
public async Task ReadInTestOutputIfSomethingIsLogged()
{
var response = await _client.GetAsync("/WeatherForecast");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
public void Dispose()
{
_factory.Dispose();
_client.Dispose();
}
}
}
As a result, "This should be logged during testing" will be shown in the test output.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Microsoft.AspNetCore.Mvc.Testing (>= 6.0.0 && <= 6.0.99)
- xunit.abstractions (>= 2.0.3 && <= 3.2.0)
-
net7.0
- Microsoft.AspNetCore.Mvc.Testing (>= 7.0.0 && <= 7.0.99)
- xunit.abstractions (>= 2.0.3 && <= 3.2.0)
-
net8.0
- Microsoft.AspNetCore.Mvc.Testing (>= 8.0.0 && <= 8.0.99)
- xunit.abstractions (>= 2.0.3 && <= 3.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.2.0-net9support | 39 | 5/20/2024 |
1.1.0 | 489 | 11/14/2023 |
1.0.3 | 94 | 10/19/2023 |
1.0.2-preview | 56 | 10/19/2023 |
1.0.1-preview | 99 | 10/18/2023 |
1.0.0-preview | 97 | 10/18/2023 |
Full support for .NET 8 (no rc or preview)