Closed bastbu closed 2 years ago
A quick spike with the below logger shows that using the ITestOutputHelper
correlates the standard output with the test case when running in VS. On the console (if using dotnet test --logger "console;verbosity=detailed"
the statements captured by ITestOutputHelper
are not automatically prefixed/correlatable with a test case.
This logger:
private class TestOutputLogger<T> : ILogger<T>
{
private const LogLevel TestLogLevel = LogLevel.Debug;
private readonly ITestOutputHelper testOutputHelper;
public TestOutputLogger(ITestOutputHelper testOutputHelper) =>
this.testOutputHelper = testOutputHelper;
public IDisposable BeginScope<TState>(TState state) => NullDisposable.Instance;
public bool IsEnabled(LogLevel logLevel) => logLevel >= TestLogLevel;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (!IsEnabled(logLevel)) return;
var message = formatter(state, exception);
this.testOutputHelper.WriteLine(message);
}
}
made sure that the test output was correlated in the test explorer:
On the console the output was not automatically prefixed with the test case:
FunctionBundler request finished preparing.
FunctionBundler request finished preparing.
FunctionBundler request: {"GatewayId":"foo","ClientFCntUp":1,"ClientFCntDown":0,"Rssi":2.3,"AdrRequest":{"DataRate":2,"RequiredSnr":-15,"PerformADRCalculation":false,"FCntUp":1,"FCntDown":0,"MinTxPowerIndex":7,"GatewayId":"foo","ClearCache":false},"FunctionItems":5}
Description
By using
ITestOutputHelper
in xUnit we can make sure that logs can be correlated with the respective test. This makes it easier for us to debug failing tests, since the logs for the tests can be analyzed as a whole.Acceptance criteria
ITestOutputHelper
is used for logging in integration tests