MELT.Xunit 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package MELT.Xunit --version 0.2.0
NuGet\Install-Package MELT.Xunit -Version 0.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MELT.Xunit" Version="0.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MELT.Xunit --version 0.2.0
#r "nuget: MELT.Xunit, 0.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MELT.Xunit as a Cake Addin
#addin nuget:?package=MELT.Xunit&version=0.2.0

// Install MELT.Xunit as a Cake Tool
#tool nuget:?package=MELT.Xunit&version=0.2.0

Testing Library for Microsoft Extensions Logging.

Build Status alternate text is missing from this package README image

About MELT

MELT is a free, open source, testing library for the .NET Standard Microsoft Extensions Logging library. It is a solution to easily test logs.

It is a repackaging with a sweetened API and some omissions of Microsoft.Extensions.Logging.Testing, a library used internally in ASP.NET Core for testing the logging, given that there is currently no plan to offer an official package for it.

It is licensed under Apache License 2.0. Most of the code is copyrighted by the .NET Foundation as mentioned in the files headers.

If you like this project please don't forget to star it on GitHub or let me know with a tweet.

Quick start

  • Install the NuGet package MELT

    <PackageReference Include="MELT" Version="0.1.0" />
    
  • Get a test logger factory

    var loggerFactory = MELTBuilder.CreateLoggerFactory();
    
  • Get a logger from the factory as usual, to pass ro your fixture.

    var logger = loggerFactory.CreateLogger<Sample>();
    
Assert log entries

The logger factory expose a property LogEntries that enumerates all the logs captured. Each entry expose all the relevant property for a log.

For example, to test with xUnit that a single log has been emitted and it had a specific message:

var log = Assert.Single(loggerFactory.LogEntries);
Assert.Equal("The answer is 42", log.Message);
Assert scopes

The logger factory expose a property Scopes that enumerates all the scopes captured.

For example, to test with xUnit that a single scope has been emitted and it had a specific message:

var scope = Assert.Single(loggerFactory.Scopes);
Assert.Equal("I'm in the GET scope", scope.Message);

There is also a property Scope in each log entry to have the scope captured with that entry.

Easily test log or scope properties with xUnit
  • Install the NuGet package MELT.Xunit

    <PackageReference Include="MELT.Xunit" Version="0.1.0" />
    
  • Use the LogValuesAssert.Contains(...) helpers. For example, to test that a single log has been emitted and it had a property number with value 42:

    var log = Assert.Single(loggerFactory.LogEntries);
    LogValuesAssert.Contains("number", 42, log.Properties);
    
Full example

See SampleTest.

Quick start for ASP.NET Core integration tests

  • Install the NuGet package MELT

    <PackageReference Include="MELT" Version="0.1.0" />
    
  • Create a test sink using MELTBuilder.CreateTestSink(...), where you can also customize the bahaviour.

    For example to filter all log entries and scopes not generated by loggers consumed in the SampleWebApplication.* namespace (this filters the logger name so it assumes you are using ILogger<T> or following the default naming convention for your loggers.)

    ITestSink _sink = MELTBuilder.CreateTestSink(options => options.FilterByNamespace(nameof(SampleWebApplication)));
    

    You can also filter by logger name using FilterByTypeName<T>() or FilterByLoggerName(string name).

  • Use the AddTestLogger(...) extension method to add the test logger provider to the logging builder. This can be done where you are already configuring the web host builder.

    Configure the logger using WithWebHostBuilder on the factory.

    using using Microsoft.Extensions.Logging;
    // ...
    factory = factory.WithWebHostBuilder(builder => builder.ConfigureLogging(logging => logging.AddTestLogger(_sink)));
    

    Alternatively, you can configure the logger builder in the ConfigureWebHost implementation of your custom WebApplicationFactory<T>. If you chose so, the same sink will be used by all tests using the same factory. You can clear the sink in the test costrctuctor with Clear() if you like to have a clean state before each test, as xUnit will not run tests consuming the same fixture in parallel.

    The logger will be automatically injected with Dependency Injection.

Assert log entries

The sink expose a property LogEntries that enumerates all the logs captured. Each entry expose all the relevant property for a log.

For example, to test with xUnit that a single log has been emitted and it had a specific message:

var log = Assert.Single(sink.LogEntries);
Assert.Equal("The answer is 42", log.Message);
Assert scopes

The sink expose a property Scopes that enumerates all the scopes captured.

For example, to test with xUnit that a single scope has been emitted and it had a specific message:

var scope = Assert.Single(sink.Scopes);
Assert.Equal("I'm in the GET scope", scope.Message);

There is also a property Scope in each log entry to have the scope captured with that entry.

Easily test log or scope properties with xUnit
  • Install the NuGet package MELT.Xunit

    <PackageReference Include="MELT.Xunit" Version="0.1.0" />
    
  • Use the LogValuesAssert.Contains(...) helpers. For example, to test that a single log has been emitted and it had a property number with value 42:

    var log = Assert.Single(sink.LogEntries);
    LogValuesAssert.Contains("number", 42, log.Properties);
    
Full example

See LoggingTest.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on MELT.Xunit:

Package Downloads
Reductech.Sequence.Core.TestHarness

Class library for automatically testing Sequence® Core steps.

Sequence.Core.TestHarness

Class library for automatically testing Sequence Core steps.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.9.0 84,654 8/6/2023
0.8.0 217,594 4/26/2021
0.7.0 8,766 2/21/2021
0.6.0 1,758 11/7/2020
0.6.0-preview.1 222 10/16/2020
0.5.3 17,297 8/31/2020
0.5.2 462 8/30/2020
0.5.1 443 8/30/2020
0.5.0 456 8/30/2020
0.3.0 6,135 11/24/2019
0.2.0 511 11/3/2019
0.1.0 638 10/26/2019