Contains xUnit logging utilities, like:
- Adds and
AssertableLoggerProvider
that forward logs to anIList<string>
object, making it easy to assert its content. - Adds an
XunitTestOutputLoggerProvider
that forward logs to xUnitITestOutputHelper
.
dotnet add package ForEvolve.Testing.Logging
In your test method, create a IList<string>
then register the logging provider, with your IHostBuilder
like:
public class BaseAbstractions
{
[Fact]
public void Should_log_the_Service_Execute_line()
{
// Arrange
var lines = new List<string>();
var args = new string[0];
var host = Host.CreateDefaultBuilder(args)
.ConfigureLogging(loggingBuilder =>
{
loggingBuilder.ClearProviders();
loggingBuilder.AddAssertableLogger(lines);
})
.ConfigureServices(services =>
{
services.AddSingleton<IService, Service>();
})
.Build();
var service = host.Services.GetRequiredService<IService>();
// Act
service.Execute();
// Assert
Assert.Collection(lines,
line => Assert.Equal("Service.Execute()", line)
);
}
public interface IService
{
void Execute();
}
public class Service : IService
{
private readonly ILogger _logger;
public Service(ILogger<Service> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public void Execute()
{
_logger.LogInformation("Service.Execute()");
}
}
}
Take into consideration that in more complex tests, more logs could be sent to the logger (like system logs). Filtering logs could be a good strategy there.
In your test class, you must inject an ITestOutputHelper
, then register the logging provider, with your IHostBuilder
like:
public class ProvidersTest
{
private readonly ITestOutputHelper _output;
public Providers(ITestOutputHelper output)
{
_output = output ?? throw new ArgumentNullException(nameof(output));
}
[Fact]
public async Task CustomizeProvidersAsync()
{
var args = new string[0];
var host = Host.CreateDefaultBuilder(args)
.ConfigureLogging(loggingBuilder =>
{
loggingBuilder.AddxUnitTestOutput(_output);
})
.Build();
var tokenSource = new CancellationTokenSource(1000);
await host.RunAsync(tokenSource.Token);
}
}
Running that test should output something like:
Application started. Press Ctrl+C to shut down.
Hosting environment: Production
Content root path: [the path of your project]\bin\Debug\net5.0
Application is shutting down...
Please open an issue and be as clear as possible; see How to contribute? for more information.
If you would like to contribute to the project, first, thank you for your interest, and please read Contributing to ForEvolve open source projects for more information.
Also, please read the Contributor Covenant Code of Conduct that applies to all ForEvolve repositories.