Maris.Logging.Testing 1.0.0-Beta2

Prefix Reserved
This is a prerelease version of Maris.Logging.Testing.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Maris.Logging.Testing --version 1.0.0-Beta2
                    
NuGet\Install-Package Maris.Logging.Testing -Version 1.0.0-Beta2
                    
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="Maris.Logging.Testing" Version="1.0.0-Beta2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Maris.Logging.Testing" Version="1.0.0-Beta2" />
                    
Directory.Packages.props
<PackageReference Include="Maris.Logging.Testing" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Maris.Logging.Testing --version 1.0.0-Beta2
                    
#r "nuget: Maris.Logging.Testing, 1.0.0-Beta2"
                    
#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.
#addin nuget:?package=Maris.Logging.Testing&version=1.0.0-Beta2&prerelease
                    
Install Maris.Logging.Testing as a Cake Addin
#tool nuget:?package=Maris.Logging.Testing&version=1.0.0-Beta2&prerelease
                    
Install Maris.Logging.Testing as a Cake Tool

Maris.Logging.Testing

Apache License v2 Maris.Logging.Testing Release date

Microsoft.Extensions.Logging.ILogger または Microsoft.Extensions.Logging.ILogger<TCategoryName> の xUnit テストで利用可能なロガーを提供します。 このロガーを使用すると、テスト対象クラスで出力したログを、 Visual Studio のテストエクスプローラー上で確認できるようになります。 また Microsoft.Extensions.Logging.Testing.FakeLogger と連携して、テストコード内でログ出力内容を検証する機能を提供します。

インストール方法

パッケージマネージャーコンソールまたはコマンドプロンプトで以下のコマンドを実行してインストールしてください。

  • パッケージマネージャーコンソール
Install-Package Maris.Logging.Testing
  • コマンドプロンプト
dotnet add package Maris.Logging.Testing

使用方法

以下のような ILogger<TCategoryName> のオブジェクトを必要とするクラスに対して、 xUnit のテストコードを記述する場合を考えます。

using Microsoft.Extensions.Logging;

namespace Maris;

public class TestTarget
{
    private readonly ILogger<TestTarget> Logger;

    public TestTarget(ILogger<TestTarget> logger)
        => this.Logger = logger;
    
    // 省略
}

テストコードでテスト用のロガーを利用するためには、以下の手順で実装します。

  1. xUnit v3 のプロジェクトテンプレートをインストール して、 dotnet new xunit3 コマンドで xUnit v3 テストプロジェクトを作成します。
  2. xUnit のテストプロジェクトに Maris.Logging.Testing のパッケージをインストールします。
  3. テストクラスのコンストラクターを定義し、 Xunit.ITestOutputHelper インターフェースのオブジェクトを引数に取ります。
  4. コンストラクターで Maris.Logging.Testing.Xunit.TestLoggerManager のオブジェクトを生成し、フィールドに保存します。 TestLoggerManager のコンストラクターには ITestOutputHelper のオブジェクトを渡します。
  5. テストメソッド内で TestLoggerManagerCreateLogger メソッドを呼び出して、テスト対象クラスに渡す ILogger または ILogger<TCategoryName> のオブジェクトを生成します。
using Maris.Logging.Testing.Xunit;
using Xunit;

namespace Maris.Tests;

public class TestClass1
{
    private readonly TestLoggerManager loggerManager;

    public TestClass1(ITestOutputHelper testOutputHelper)
        => this.loggerManager = new TestLoggerManager(testOutputHelper);

    [Fact]
    public void TestMethod()
    {
        // ILogger<T> のオブジェクトを生成可能。
        var logger = this.loggerManager.CreateLogger<TestTarget>();
        var target = new TestTarget(logger);

        // Do something...

        // TestLoggerManagerのLogCollectorプロパティはFakeLogger.Collectorを公開します
        Assert.Equal(1, this.loggerManager.LogCollector.Count);  
        Assert.Equal("expectedCategory", this.loggerManager.LogCollector.LatestRecord.Category);
        Assert.Equal("expectedMessage", this.loggerManager.LogCollector.LatestRecord.Message);
    }
}

Microsoft.Extensions.Hosting.IHost のテストを行う際は、 AddTestLogging メソッドを利用してテスト用のロガーを DI コンテナーに登録できます。 AddTestLogging メソッドは Microsoft.Extensions.DependencyInjection.TestLoggerServiceCollectionExtensions クラスに定義されています。 xUnit のテストコード例は以下のとおりです。

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Maris.Logging.Testing.Xunit;
using Xunit;

namespace Maris.Tests;

public class TestClass2
{
    private readonly TestLoggerManager loggerManager;

    public TestClass2(ITestOutputHelper testOutputHelper)
        => this.loggerManager = new TestLoggerManager(testOutputHelper);

    [Fact]
    public async Task TestMethod()
    {
        using var app = this.CreateHost();
        var cancellationToken = TestContext.Current.CancellationToken;
        await app.RunAsync(cancellationToken);
        
        // Do something...
    }

    private IHost CreateHost()
    {
        var builder = Host.CreateDefaultBuilder();
        builder.ConfigureServices((context, services) =>
        {
            // テスト用の Logger を登録
            services.AddTestLogging(this.loggerManager);

            // Do something...            
        });

        return builder.Build();
    }
}

テストエクスプローラーではテスト実行中に出力されたログが以下のように表示されます。

test-explorer-log

詳細は ASP.NET Core での統合テスト を参照してください。

主な依存ライブラリ

ライセンス

Apache License Version 2.0

Product 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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 646 a month ago
1.0.0-Beta5 130 a month ago
1.0.0-Beta4 131 a month ago
1.0.0-Beta3 449 2 months ago
1.0.0-Beta2 446 2 months ago