xunit version | Package | NuGet Version |
---|---|---|
xunit v2 | MartinCostello.Logging.XUnit | |
xunit v3 | MartinCostello.Logging.XUnit.v3 |
MartinCostello.Logging.XUnit
and MartinCostello.Logging.XUnit.v3
provide extensions to hook into
the ILogger
infrastructure to output logs from your xunit tests to the test output.
Projects using xunit v2 should use the MartinCostello.Logging.XUnit
package, while projects using
xunit v3 should use the MartinCostello.Logging.XUnit.v3
package.
Note
This library is designed for the Microsoft logging implementation of ILoggerFactory
.
For other logging implementations, such as Serilog, consider using packages such as Serilog.Sinks.XUnit instead.
To install the library from NuGet using the .NET SDK run one of the following commands.
dotnet add package MartinCostello.Logging.XUnit
dotnet add package MartinCostello.Logging.XUnit.v3
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions; // For xunit v2 - not required for xunit v3
namespace MyApp.Calculator;
public class CalculatorTests(ITestOutputHelper outputHelper)
{
[Fact]
public void Calculator_Sums_Two_Integers()
{
// Arrange
using var serviceProvider = new ServiceCollection()
.AddLogging((builder) => builder.AddXUnit(outputHelper))
.AddSingleton<Calculator>()
.BuildServiceProvider();
var calculator = services.GetRequiredService<Calculator>();
// Act
int actual = calculator.Sum(1, 2);
// Assert
Assert.AreEqual(3, actual);
}
}
public sealed class Calculator(ILogger<Calculator> logger)
{
public int Sum(int x, int y)
{
int sum = x + y;
logger.LogInformation("The sum of {x} and {y} is {sum}.", x, y, sum);
return sum;
}
}
See below for links to more examples:
Xunit v3 contains many major architectural changes which means the same package that supports logging for xunit v2 cannot be used with xunit v3. The equivalent NuGet package to support logging for xunit v3 is the new MartinCostello.Logging.XUnit.v3 package.
To migrate usage of MartinCostello.Logging.XUnit
to MartinCostello.Logging.XUnit.v3
for xunit v3:
-
Follow the relevant steps to migrate any test projects from xunit v2 to v3.
- The most relevant change in xunit v3 is that the
ITestOutputHelper
type has moved from theXunit.Abstractions
namespace toXunit
.
- The most relevant change in xunit v3 is that the
-
Change any package references from
MartinCostello.Logging.XUnit
toMartinCostello.Logging.XUnit.v3
.- <PackageReference Include="MartinCostello.Logging.XUnit" Version="0.5.0" /> + <PackageReference Include="MartinCostello.Logging.XUnit.v3" Version="0.5.0" />
Any feedback or issues can be added to the issues for this project in GitHub.
The repository is hosted in GitHub: https://github.com/martincostello/xunit-logging.git
This project is licensed under the Apache 2.0 license.
Compiling the solution yourself requires Git and the .NET SDK to be installed (version 9.0.100
or later).
To build and test the solution locally from a terminal/command-line, run the following set of commands:
git clone https://github.com/martincostello/xunit-logging.git
cd xunit-logging
./build.ps1