DotNetToolbox.TestUtilities
8.1.1
dotnet add package DotNetToolbox.TestUtilities --version 8.1.1
NuGet\Install-Package DotNetToolbox.TestUtilities -Version 8.1.1
<PackageReference Include="DotNetToolbox.TestUtilities" Version="8.1.1" />
paket add DotNetToolbox.TestUtilities --version 8.1.1
#r "nuget: DotNetToolbox.TestUtilities, 8.1.1"
// Install DotNetToolbox.TestUtilities as a Cake Addin #addin nuget:?package=DotNetToolbox.TestUtilities&version=8.1.1 // Install DotNetToolbox.TestUtilities as a Cake Tool #tool nuget:?package=DotNetToolbox.TestUtilities&version=8.1.1
TestUtilities (DotNetToolbox.TestUtilities)
Introduction
DotNetToolbox.TestUtilities is a comprehensive C# library for .NET 8, designed to enhance logging and tracking capabilities in testing environments. It provides robust tools for inspecting, verifying, and validating log outputs. The library includes classes that can be used as mocked instances in test cases where a test subject requires ILogger
, ILoggerFactory
, and ILoggerProvider
to be injected. This makes it particularly useful for unit testing scenarios where you need to verify logging behavior or when testing components that interact with logging services.
Table of Contents
Installation
PM> Install-Package DotNetToolbox.TestUtilities
Dependencies
- .NET 8
Features
- ITrackedLogger: Interface for loggers that track and store log entries.
- Log: Data structure for representing individual log entries.
- TrackedLogger: Extends basic logging to include tracking of log messages.
- TrackedNullLogger: An in-memory only logger for capturing and inspecting logs during tests.
- TrackedLoggerFactory: Factory for creating instances of
TrackedLogger
. - TrackedNullLoggerFactory: Factory for creating instances of
TrackedNullLogger
. - TrackedLoggerProvider: Provides
TrackedLogger
instances, integrating with existing logging systems.
Usage
To use DotNetToolbox.TestUtilities, include it in your test project and instantiate the desired logging components, such as TrackedNullLogger
or TrackedLogger
, as per your testing requirements.
Examples
Here are some examples demonstrating the usage of the library:
Implementing TrackedLogger:
var trackedLogger = new TrackedLogger<MyClass>(logger); trackedLogger.Log(LogLevel.Warning, new EventId(2, "WarningEvent"), "Warning message", null, Formatter);
Creating Log Entries:
var logEntry = new Log(LogLevel.Error, new EventId(3, "ErrorEvent"), "Error message", new Exception("TestException"), "Formatted message");
Using TrackedNullLogger:
var logger = TrackedNullLogger<MyClass>.Instance; logger.Log(LogLevel.Information, new EventId(1, "TestEvent"), "Test message", null, Formatter);
Mocking ILoggerFactory for a Class Constructor:
public class MyClass { private readonly ILogger _logger; public MyClass(ILoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger<MyClass>(); } // Class methods } // In your test var loggerFactory = new TrackedLoggerFactory(Substitute.For<ILoggerFactory>()); var myClassInstance = new MyClass(loggerFactory);
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. |
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on DotNetToolbox.TestUtilities:
Package | Downloads |
---|---|
DotNetToolbox.FluentAssertions
An extension of FluentAssertions that provides specialized assertion capabilities for logging components. |
GitHub repositories
This package is not used by any popular GitHub repositories.
DotNetToolbox.TestUtilities Version 8.1.1:
Stable release of the DotNetToolbox.TestUtilities library, a comprehensive C# library for .NET 8, designed to enhance logging and tracking capabilities in testing environments.