Azure / iotedge-lorawan-starterkit

Sample implementation of LoRaWAN components to connect LoRaWAN antenna gateway running IoT Edge directly with Azure IoT.
https://azure.github.io/iotedge-lorawan-starterkit/2.2.1
Other
184 stars 63 forks source link

Ensure that test logs can be correlated with test case #808

Closed bastbu closed 2 years ago

bastbu commented 2 years ago

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

bastbu commented 2 years ago

https://xunit.net/docs/capturing-output

bastbu commented 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:

image

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}