Intercode.Toolbox.UnitTesting.XUnit
2.5.0
dotnet add package Intercode.Toolbox.UnitTesting.XUnit --version 2.5.0
NuGet\Install-Package Intercode.Toolbox.UnitTesting.XUnit -Version 2.5.0
<PackageReference Include="Intercode.Toolbox.UnitTesting.XUnit" Version="2.5.0" />
<PackageVersion Include="Intercode.Toolbox.UnitTesting.XUnit" Version="2.5.0" />
<PackageReference Include="Intercode.Toolbox.UnitTesting.XUnit" />
paket add Intercode.Toolbox.UnitTesting.XUnit --version 2.5.0
#r "nuget: Intercode.Toolbox.UnitTesting.XUnit, 2.5.0"
#:package Intercode.Toolbox.UnitTesting.XUnit@2.5.0
#addin nuget:?package=Intercode.Toolbox.UnitTesting.XUnit&version=2.5.0
#tool nuget:?package=Intercode.Toolbox.UnitTesting.XUnit&version=2.5.0
Intercode.Toolbox.UnitTesting.UnitTesting.XUnit
A trimmable, AOT-compatible .NET library that provides logging and dependency injection tools for unit testing using the XUnit library.
XUnitLogger class
The XUnitLogger class implements the ILogger
interface and logs messages to a ITestOutputHelper test output.<br>
Even the XUnitLogger can be created directly as show below, it is recommended to use XUnitLoggerProvider to create instances
of the logger; or better yet, use obtain the logger through Dependency Injection by using deriving from the DependencyInversionTestBase
abstract class.
Usage
// _outputHelper is an instance of ITestOutputHelper, passed in through the constructor of the test fixture class
var logger = new XUnitLogger( _outputHelper, "Test", LogLevel.Information );
logger.LogInformation( "This is a test" );
Reference
- Initialize a new instance of the
XUnitLoggerclass. ThecategoryNameparameter is used to filter log messages and thelogLevelparameter is used to filter log messages by severity. If a category name is not provided, an empty string will be used.
XUnitLogger(
ITestOutputHelper output,
string? categoryName = null,
LogLevel logLevel = LogLevel.Error )
Logwrites a log entry to the XUnit test output.
void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception? exception,
Func<TState, Exception?, string> formatter )
IsEnabledchecks if the given log level is enabled for logging.
bool IsEnabled( LogLevel logLevel )
BeginScopebegins a logical operation scope. The return scope object must be disposed to end the scope.
IDisposable BeginScope<TState>(
TState state )
where TState: notnull
XUnitLoggerProvider class
The XUnitLoggerProvider class is used to create XUnitLogger instances and implements the ILoggerProvider
interface. The logger provider must be disposed when it's no longer needed.<br>
Even the XUnitLoggerProvider can be created directly as show below, it is recommended to obtain loggers through Dependency Injection by using deriving from the DependencyInversionTestBase
abstract class.
Usage
// _outputHelper is an instance of ITestOutputHelper, passed in through the constructor of the test fixture class
using var provider = new XUnitLoggerProvider( _output, LogLevel.Information );
var logger = provider.CreateLogger( "Test" );
logger.LogInformation( "This is a test" );
Reference
- Create a new instance of the
XUnitLoggerProviderclass. The value for theoutputHelperparameter is usually passed in to the test fixture's constructor. ThelogLevelparameter is used to filter log messages by severity.
XUnitLoggerProvider(
ITestOutputHelper outputHelper,
LogLevel logLevel = LogLevel.Error )
CreateLoggercreates a new logger instance with the specified category name; it will return an existing logger if one with the same category name already exists.
ILogger CreateLogger( string categoryName )
DependencyInversionTestBase class
The DependencyInversionTestBase class is an abstract class that provides logging and dependency injection services for unit test fixtures.
Usage
public class MyTestFixture : DependencyInversionTestBase
{
public MyTestFixture( ITestOutputHelper outputHelper )
: base( outputHelper, typeof(ILogger<MyTestFixture>), AddServices )
{
}
[Fact]
public void MyTest()
{
var dbContext = ServiceProvider.GetRequiredService<TestDbContext>();
// Perform test
}
private static void AddServices( IServiceCollection services )
{
services.AddDbContext<TestDbContext>(
options =>
{
var connection = new SqliteConnection( "Data Source=:memory:" );
connection.Open();
options.UseSqlite( connection );
}
);
}
}
Reference
Constructor
Initialize a new instance of the DependencyInversionTestBase class. The outputHelper parameter is destination for log messages.
The loggerType parameter is used to specify the type of logger to use. The addServices parameter is a delegate that is used to configure the service provider.
The configurationValues and configureSource parameters are used to configure the configuration provider.
See the DependencyInjection.CreateConfiguration method in the Intercode.Toolbox.UnitTesting package for more information.
DependencyInversionTestBase(
ITestOutputHelper outputHelper,
Type loggerType,
LogLevel logLevel = LogLevel.Information,
Action<IServiceCollection>? servicesSetter = null,
Dictionary<string, string?>? configurationValues = null,
Action<IConfigurationBuilder>? configureSource = null )
Properties
ServiceProvidergets the service provider that was created for the test fixture.
IServiceProvider ServiceProvider { get; }
Loggergets the logger that was created for the test fixture. The logger can also be obtained by callingServiceProvider.GetRequiredService<ILogger<T>>().
ILogger Logger { get; }
License
This project is licensed under the MIT License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Intercode.Toolbox.UnitTesting (>= 2.5.0)
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Logging.Debug (>= 8.0.0)
- xunit.abstractions (>= 2.0.3)
-
net9.0
- Intercode.Toolbox.UnitTesting (>= 2.5.0)
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Logging.Debug (>= 8.0.0)
- xunit.abstractions (>= 2.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.