Test framework logging
This page describes functionality related to logging in tests.
- xUnit
- NUnit
- MSTest
The Arcus.Testing.Logging.Xunit
library provides a XunitTestLogger
type that's an implementation of the abstracted Microsoft Ilogger
inside the xUnit test framework.
Installation
The following functionality is available when installing this package:
PM> Install-Package -Name Arcus.Testing.Logging.Xunit # for xUnit v2 tests
PM> Install-Package -Name Arcus.Testing.Logging.Xunit.v3 # for xUnit v3 tests
Example
Log messages written to the ILogger
instance will be written to the xUnit test output.
using Arcus.Testing;
using Microsoft.Extensions.Logging;
using Xunit;
public class TestClass
{
private readonly ILogger _testLogger;
public TestClass(ITestOutputHelper outputWriter)
{
_testLogger = new XunitTestLogger(outputWriter);
}
}
In the same fashion there is an extension to add a ILoggerProvider
to a Microsoft Logging setup:
using Arcus.Testing;
using Microsoft.Extensions.Logging;
ILoggerFactory factory = LoggerFactory.Create(logging =>
{
logging.SetMinimumLevel(LogLevel.Warning)
.AddFilter("Example.DefaultService", LogLevel.Trace)
.AddFile("logs/test-{0:yyyy}-{0:MM}-{0:dd}.log")
.AddXunitTestLogging(outputWriter);
});
ILogger logger = factory.CreateLogger<TestClass>();
The Arcus.Testing.Logging.NUnit
library provides a NUnitTestLogger
type that's an implementation of the abstracted Microsoft Ilogger
inside the NUnit test framework.
Installation
The following functionality is available when installing this package:
PM> Install-Package -Name Arcus.Testing.Logging.NUnit
Example
Log messages written to the ILogger
instance will be written to the TestContext.Out/Error
in the NUnit test output.
using Arcus.Testing;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
public class TestClass
{
private readonly ILogger _testLogger;
public TestClass()
{
_testLogger = new NUnitTestLogger(TestContext.Out, TestContext.Error);
}
}
In the same fashion there is an extension to add a ILoggerProvider
to a Microsoft Logging setup:
using Arcus.Testing;
using Microsoft.Extensions.Logging;
ILoggerFactory factory = LoggerFactory.Create(logging =>
{
logging.SetMinimumLevel(LogLevel.Warning)
.AddFilter("Example.DefaultService", LogLevel.Trace)
.AddFile("logs/test-{0:yyyy}-{0:MM}-{0:dd}.log")
.AddNUnitTestLogging(TestContext.Out, TestContext.Error);
});
ILogger logger = factory.CreateLogger<TestClass>();
The Arcus.Testing.Logging.MSTest
library provides a MSTestLogger
type that's an implementation of the abstracted Microsoft Ilogger
inside the MSTest test framework.
Installation
The following functionality is available when installing this package:
PM> Install-Package -Name Arcus.Testing.Logging.MSTest
Example
Log messages written to the ILogger
instance will be written to the TestContext
in the MSTest test output.
using Arcus.Testing;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
public class TestClass
{
private ILogger TestLogger => new MSTestLogger(TestContext);
public TestContext TestContext { get; set; }
}
In the same fashion there is an extension to add a ILoggerProvider
to a Microsoft Logging setup:
using Arcus.Testing;
using Microsoft.Extensions.Logging;
ILoggerFactory factory = LoggerFactory.Create(logging =>
{
logging.SetMinimumLevel(LogLevel.Warning)
.AddFilter("Example.DefaultService", LogLevel.Trace)
.AddFile("logs/test-{0:yyyy}-{0:MM}-{0:dd}.log")
.AddMSTestLogging(TestContext);
});
ILogger logger = factory.CreateLogger<TestClass>();