Dotnet Testing Test Output Logging logo

Dotnet Testing Test Output Logging

Community
kevintsengtw
dotnet-testing-test-output-logging

xUnit 測試輸出與記錄完整指南。當需要在 xUnit 測試中實作測試輸出、診斷記錄或 ILogger 替代品時使用。涵蓋 ITestOutputHelper 注入、AbstractLogger 模式、結構化輸出設計。包含 XUnitLogger、CompositeLogger、效能測試診斷工具實作。 Keywords: ITestOutputHelper, ILogger testing, test output xunit, 測試輸出, 測試記錄, AbstractLogger, XUnitLogger, CompositeLogger, testOutputHelper.WriteLine, 測試診斷, logger mock, 測試日誌, 結構化輸出, Received().Log

Overview

Publisherkevintsengtw
Repositorydotnet-testing-agent-skills
Skill namedotnet-testing-test-output-logging
Stars
30
Forks
4
Bundled files
3
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 3 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by kevintsengtw on GitHub. Read the source before you install it.

Installation

Install the Dotnet Testing Test Output Logging AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/kevintsengtw/dotnet-testing-agent-skills/tree/main/skills/dotnet-testing-test-output-logging
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/kevintsengtw/dotnet-testing-agent-skills.git /tmp/dotnet-testing-agent-skills
mkdir -p .claude/skills
cp -r /tmp/dotnet-testing-agent-skills/skills/dotnet-testing-test-output-logging .claude/skills/dotnet-testing-test-output-logging
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Dotnet Testing Test Output Logging in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Dotnet Testing Test Output Logging on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Dotnet Testing Test Output Logging is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

測試輸出與記錄專家指南

本技能協助您在 .NET xUnit 測試專案中實作高品質的測試輸出與記錄機制。

核心原則

1. ITestOutputHelper 使用原則

正確的注入方式

  • 透過建構式注入 ITestOutputHelper
  • 每個測試類別的實例與測試方法綁定
  • 不可在靜態方法或跨測試方法間共用
csharp
public class MyTests
{
    private readonly ITestOutputHelper _output;
    
    public MyTests(ITestOutputHelper testOutputHelper)
    {
        _output = testOutputHelper;
    }
}

常見錯誤

  • 靜態存取:private static ITestOutputHelper _output
  • 在非同步測試中未等待即使用
  • 嘗試在 Dispose 方法中使用

2. 結構化輸出格式設計

建議的輸出結構

csharp
private void LogSection(string title)
{
    _output.WriteLine($"\n=== {title} ===");
}

private void LogKeyValue(string key, object value)
{
    _output.WriteLine($"{key}: {value}");
}

private void LogTimestamp(DateTime time)
{
    _output.WriteLine($"執行時間: {time:yyyy-MM-dd HH:mm:ss.fff}");
}

輸出時機

  • 測試開始時:記錄測試設置與輸入資料
  • 執行過程中:記錄重要的狀態變化
  • 斷言前:記錄預期值與實際值
  • 測試結束時:記錄執行時間與結果摘要

3. ILogger 測試策略

挑戰:擴充方法無法直接 Mock

ILogger.LogError() 是擴充方法,NSubstitute 無法直接攔截。需要攔截底層的 Log<TState> 方法:

csharp
// ❌ 錯誤:直接 Mock 擴充方法會失敗
logger.Received().LogError(Arg.Any<string>());

// ✅ 正確:攔截底層方法
logger.Received().Log(
    LogLevel.Error,
    Arg.Any<EventId>(),
    Arg.Is<object>(o => o.ToString().Contains("預期訊息")),
    Arg.Any<Exception>(),
    Arg.Any<Func<object, Exception, string>>()
);

解決方案:使用抽象層

建立 AbstractLogger<T> 來簡化測試:

csharp
public abstract class AbstractLogger<T> : ILogger<T>
{
    public IDisposable BeginScope<TState>(TState state) 
        => null;
    
    public bool IsEnabled(LogLevel logLevel) 
        => true;
    
    public void Log<TState>(
        LogLevel logLevel,
        EventId eventId,
        TState state,
        Exception exception,
        Func<TState, Exception, string> formatter)
    {
        Log(logLevel, exception, state?.ToString() ?? string.Empty);
    }
    
    public abstract void Log(LogLevel logLevel, Exception ex, string information);
}

測試時使用

csharp
var logger = Substitute.For<AbstractLogger<MyService>>();
// 現在可以簡單驗證
logger.Received().Log(LogLevel.Error, Arg.Any<Exception>(), Arg.Is<string>(s => s.Contains("錯誤訊息")));

4. 診斷工具整合

XUnitLogger:將記錄導向測試輸出

csharp
public class XUnitLogger<T> : ILogger<T>
{
    private readonly ITestOutputHelper _testOutputHelper;
    
    public XUnitLogger(ITestOutputHelper testOutputHelper)
    {
        _testOutputHelper = testOutputHelper;
    }
    
    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, 
        Exception exception, Func<TState, Exception, string> formatter)
    {
        var message = formatter(state, exception);
        _testOutputHelper.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] [{logLevel}] [{typeof(T).Name}] {message}");
        if (exception != null)
        {
            _testOutputHelper.WriteLine($"Exception: {exception}");
        }
    }
    
    // 其他必要的介面實作...
}

CompositeLogger:同時支援驗證與輸出

csharp
public class CompositeLogger<T> : ILogger<T>
{
    private readonly ILogger<T>[] _loggers;
    
    public CompositeLogger(params ILogger<T>[] loggers)
    {
        _loggers = loggers;
    }
    
    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state,
        Exception exception, Func<TState, Exception, string> formatter)
    {
        foreach (var logger in _loggers)
        {
            logger.Log(logLevel, eventId, state, exception, formatter);
        }
    }
    
    // 其他介面實作會委派給所有內部 logger...
}

使用方式

csharp
// 同時進行行為驗證與測試輸出
var mockLogger = Substitute.For<AbstractLogger<MyService>>();
var xunitLogger = new XUnitLogger<MyService>(_output);
var compositeLogger = new CompositeLogger<MyService>(mockLogger, xunitLogger);

var service = new MyService(compositeLogger);

實作指南

效能測試中的時間點記錄

csharp
[Fact]
public async Task ProcessLargeDataSet_效能測試()
{
    // Arrange
    var stopwatch = Stopwatch.StartNew();
    var checkpoints = new List<(string Stage, TimeSpan Elapsed)>();
    
    _output.WriteLine("開始處理大型資料集...");
    
    // Act & Monitor
    await processor.LoadData(dataSet);
    checkpoints.Add(("資料載入", stopwatch.Elapsed));
    _output.WriteLine($"資料載入完成: {stopwatch.Elapsed.TotalMilliseconds:F2} ms");
    
    await processor.ProcessData();
    checkpoints.Add(("資料處理", stopwatch.Elapsed));
    _output.WriteLine($"資料處理完成: {stopwatch.Elapsed.TotalMilliseconds:F2} ms");
    
    stopwatch.Stop();
    
    // Assert & Report
    _output.WriteLine("\n=== 效能報告 ===");
    foreach (var (stage, elapsed) in checkpoints)
    {
        _output.WriteLine($"{stage}: {elapsed.TotalMilliseconds:F2} ms");
    }
}

診斷測試基底類別

csharp
public abstract class DiagnosticTestBase
{
    protected readonly ITestOutputHelper Output;
    
    protected DiagnosticTestBase(ITestOutputHelper output)
    {
        Output = output;
    }
    
    protected void LogTestStart(string testName)
    {
        Output.WriteLine($"\n=== {testName} ===");
        Output.WriteLine($"執行時間: {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}");
    }
    
    protected void LogTestData(object data)
    {
        Output.WriteLine($"測試資料: {JsonSerializer.Serialize(data, new JsonSerializerOptions { WriteIndented = true })}");
    }
    
    protected void LogAssertionFailure(string field, object expected, object actual)
    {
        Output.WriteLine("\n=== 斷言失敗 ===");
        Output.WriteLine($"欄位: {field}");
        Output.WriteLine($"預期值: {expected}");
        Output.WriteLine($"實際值: {actual}");
    }
}

最佳實踐

  1. 適當使用 ITestOutputHelper

    • 在複雜測試中記錄重要步驟,幫助追蹤失敗原因
    • 採用一致的結構化輸出格式(章節標題、時間戳記),讓輸出易於閱讀
    • 在效能測試中記錄時間點,提供量化數據
  2. Logger 測試策略

    • 使用抽象層(AbstractLogger)簡化測試,因為 ILogger 擴充方法無法直接 Mock
    • 驗證記錄層級而非完整訊息,避免測試因訊息文字微調而失敗
    • 使用 CompositeLogger 結合 Mock 與實際輸出,同時達成驗證與診斷

常見誤區

  1. 過度使用輸出 — 大量輸出會拖慢測試執行速度,且雜訊會淹沒有用資訊。只在複雜或易失敗的測試中加入輸出即可。記錄敏感資訊(密碼、金鑰)也是常見的安全風險。

  2. 硬編碼記錄驗證 — 驗證完整的記錄訊息字串容易因訊息微調而失敗,驗證呼叫次數則過度指定了內部實作。改為驗證記錄層級和關鍵字即可。

  3. 忽略生命週期 — ITestOutputHelper 的實例與測試方法綁定,在靜態方法或跨測試方法中使用會拋出例外。非同步測試中遺漏 await 也可能導致輸出遺失。

範例參考

參考 templates/ 目錄下的完整範例:

  • itestoutputhelper-example.cs - ITestOutputHelper 使用範例
  • ilogger-testing-example.cs - ILogger 測試策略範例
  • diagnostic-tools.cs - XUnitLogger 與 CompositeLogger 實作

輸出格式

  • 產生含 ITestOutputHelper 注入的測試類別
  • 提供 AbstractLogger/XUnitLogger/CompositeLogger 實作程式碼
  • 包含結構化輸出的 helper 方法
  • ILogger 驗證使用底層 Log 方法而非擴充方法

參考資源

原始文章

本技能內容提煉自「老派軟體工程師的測試修練 - 30 天挑戰」系列文章:

官方文件

相關技能

  • unit-test-fundamentals - 單元測試基礎
  • xunit-project-setup - xUnit 專案設定
  • nsubstitute-mocking - 測試替身與模擬

範例檔案

測試清單

在實作測試輸出與記錄時,確認以下檢查項目:

  • ITestOutputHelper 透過建構式正確注入
  • 使用結構化的輸出格式(章節、時間戳記)
  • Logger 測試使用抽象層或 CompositeLogger
  • 驗證記錄層級而非完整訊息
  • 效能測試包含時間點記錄
  • 沒有在輸出中洩漏敏感資訊
  • 非同步測試正確等待記錄完成
  • 測試失敗時提供足夠的診斷資訊

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Dotnet Testing Test Output Logging AI skill do?

xUnit 測試輸出與記錄完整指南。當需要在 xUnit 測試中實作測試輸出、診斷記錄或 ILogger 替代品時使用。涵蓋 ITestOutputHelper 注入、AbstractLogger 模式、結構化輸出設計。包含 XUnitLogger、CompositeLogger、效能測試診斷工具實作。 Keywords: ITestOutputHelper, ILogger testing, test output xunit, 測試輸出, 測試記錄, AbstractLogger, XUnitLogger, CompositeLogger, testOutputHelper.WriteLine, 測試診斷, logger mock, 測試日誌, 結構化輸出, Received().Log

Why use Dotnet Testing Test Output Logging on TypingMind?

Because you install it once and use it with any model. Dotnet Testing Test Output Logging is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Dotnet Testing Test Output Logging in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/kevintsengtw/dotnet-testing-agent-skills/tree/main/skills/dotnet-testing-test-output-logging. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Dotnet Testing Test Output Logging?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Dotnet Testing Test Output Logging?

As many as you like. As long as a model supports skills, you can use Dotnet Testing Test Output Logging with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Dotnet Testing Test Output Logging AI skill free?

Yes. It is published on GitHub by kevintsengtw under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇