Dotnet Testing Advanced Tunit Fundamentals logo

Dotnet Testing Advanced Tunit Fundamentals

Community
kevintsengtw
dotnet-testing-advanced-tunit-fundamentals

TUnit 新世代測試框架入門完整指南。當需要使用 TUnit 建立測試專案或從 xUnit 遷移至 TUnit 時使用。涵蓋 Source Generator 驅動測試發現、AOT 編譯支援、流暢式非同步斷言。包含專案建立、[Test] 屬性、生命週期管理、並行控制與 xUnit 語法對照。 Keywords: TUnit, tunit testing, source generator testing, AOT testing, 新世代測試框架, [Test], [Arguments], TUnit.Assertions, Assert.That, Before(Test), After(Test), NotInParallel, TUnit.Templates, Microsoft.Testing.Platform, TUnit vs xUnit, 並行執行

Overview

Publisherkevintsengtw
Repositorydotnet-testing-agent-skills
Skill namedotnet-testing-advanced-tunit-fundamentals
Stars
30
Forks
4
Bundled files
10
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.

  • 10 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 Advanced Tunit Fundamentals 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-advanced-tunit-fundamentals
  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-advanced-tunit-fundamentals .claude/skills/dotnet-testing-advanced-tunit-fundamentals
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Dotnet Testing Advanced Tunit Fundamentals 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 Advanced Tunit Fundamentals 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 Advanced Tunit Fundamentals 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.

TUnit 新世代測試框架入門基礎

TUnit 框架核心特色

1. Source Generator 驅動的測試發現

TUnit 與傳統測試框架最大的差異在於使用 Source Generator 在編譯時期完成測試發現,避免反射成本、完全支援 Native AOT、更快的啟動時間。

csharp
// TUnit 在編譯時期就透過 Source Generator 產生測試註冊程式碼
public class ModernTests
{
    [Test] // 編譯時期就被處理和最佳化
    public async Task TestMethod()
    {
        await Assert.That(true).IsTrue();
    }
}

2. AOT (Ahead-of-Time) 編譯支援

text
傳統 JIT:C# 原始碼 → IL 中間碼 → 執行時期 JIT 編譯 → 機器碼 → 執行
AOT:    C# 原始碼 → 編譯時期直接產生 → 機器碼 → 直接執行

AOT 優勢:超快啟動時間、更小的記憶體占用、可預測的效能、更適合容器化部署。大型專案可達 10-30 倍啟動時間改善。

3. Microsoft.Testing.Platform 採用

TUnit 建構在微軟最新的 Microsoft.Testing.Platform 之上,而非傳統的 VSTest 平台。

重要注意事項: TUnit 專案不需要也不應該安裝 Microsoft.NET.Test.Sdk 套件。

4. 預設並行執行

TUnit 將並行執行設為預設,需要時可用 [NotInParallel("GroupName")] 控制特定測試群組依序執行。

TUnit 專案建立

支援手動建立(console 模板 + TUnit 套件)或使用 TUnit Template(dotnet new tunit,推薦)。專案需設定 csproj(TUnit 套件、IsTestProject)與 GlobalUsings.cs。所有測試方法必須是非同步的(async Task)。

完整專案建立步驟與 csproj 範例請參閱 references/project-setup.md

測試屬性與參數化

基本測試 [Test]

TUnit 統一使用 [Test] 屬性,不像 xUnit 區分 [Fact] 和 [Theory]:

csharp
[Test]
public async Task Add_輸入1和2_應回傳3()
{
    var calculator = new Calculator();
    var result = calculator.Add(1, 2);
    await Assert.That(result).IsEqualTo(3);
}

參數化測試 [Arguments]

csharp
[Test]
[Arguments(1, 2, 3)]
[Arguments(-1, 1, 0)]
[Arguments(0, 0, 0)]
public async Task Add_多組輸入_應回傳正確結果(int a, int b, int expected)
{
    var calculator = new Calculator();
    var result = calculator.Add(a, b);
    await Assert.That(result).IsEqualTo(expected);
}

decimal 不是 attribute 常數型別,[Arguments(1.5m)] 會編譯失敗(CS0182)。 TUnit 0.60.15 起會把 double 或 string 常值自動轉成 decimal 參數:參數宣告為 decimal, 寫 [Arguments(1.5)] 或 [Arguments("19.99")] 即可。更早的版本不支援這個轉換, 請改用 [MethodDataSource] 提供 decimal 值。

TUnit.Assertions 斷言系統

TUnit 採用流暢式(Fluent)斷言設計,所有斷言都是非同步的。支援相等性、布林值、數值比較、字串、集合、例外等多種斷言,並可透過 And / Or 組合條件。

csharp
await Assert.That(actual).IsEqualTo(expected);
await Assert.That(email).Contains("@").And.EndsWith(".com");
await Assert.That(() => action()).Throws<InvalidOperationException>();

完整斷言類型與範例請參閱 references/tunit-assertions-detail.md

測試生命週期管理

TUnit 支援建構式 / Dispose 模式,以及 [Before(Test)]、[Before(Class)]、[After(Test)]、[After(Class)] 等屬性。

text
執行順序:Before(Class) → 建構式 → Before(Test) → 測試方法 → After(Test) → Dispose → After(Class)

完整生命週期範例與屬性對照表請參閱 references/lifecycle-management.md

並行執行控制與 xUnit 遷移

TUnit 預設並行執行所有測試,使用 [NotInParallel] 控制特定群組依序執行。提供完整的 xUnit → TUnit 語法對照表、遷移範例、CLI 執行指令與 IDE 整合設定。

完整並行控制、語法對照與遷移範例請參閱 references/xunit-migration.md

效能比較

場景xUnitTUnitTUnit AOT效能提升
簡單測試執行1,400ms1,000ms60ms23x (AOT)
非同步測試1,400ms930ms26ms54x (AOT)
並行測試1,425ms999ms54ms26x (AOT)

常見問題與解決方案

  1. 套件相容性 — 移除 Microsoft.NET.Test.Sdk,TUnit 使用新的測試平台
  2. IDE 整合問題 — 確認 IDE 版本支援 Microsoft.Testing.Platform,啟用相關預覽功能
  3. 非同步斷言遺忘 — 所有斷言都需要 await,測試方法必須是 async Task

適用場景評估

適合使用 TUnit

  1. 全新專案:沒有歷史包袱
  2. 效能要求高:大型測試套件(1000+ 測試)
  3. 技術棧先進:使用 .NET 8+,計劃採用 AOT
  4. CI/CD 重度使用:測試執行時間直接影響部署頻率
  5. 容器化部署:快速啟動時間很重要

暫時不建議

  1. Legacy 專案:已有大量 xUnit 測試
  2. 保守團隊:需要穩定性勝過創新性
  3. 複雜測試生態:大量使用 xUnit 特定套件
  4. 舊版 .NET:還在 .NET 6/7

輸出格式

  • 產生 TUnit 測試類別(含 [Test]、[Arguments] 屬性)
  • 產生 .csproj 設定(TUnit 套件參考)
  • 提供 GlobalUsings.cs 設定
  • 包含 xUnit 到 TUnit 的遷移程式碼對照

參考資源

範例檔案

原始文章

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

官方資源

Microsoft 官方文件

相關技能

  • dotnet-testing-advanced-tunit-advanced - TUnit 進階功能
  • dotnet-testing-advanced-xunit-upgrade-guide - xUnit 升級指南
  • dotnet-testing-xunit-project-setup - xUnit 專案設定(比較用)

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 Advanced Tunit Fundamentals AI skill do?

TUnit 新世代測試框架入門完整指南。當需要使用 TUnit 建立測試專案或從 xUnit 遷移至 TUnit 時使用。涵蓋 Source Generator 驅動測試發現、AOT 編譯支援、流暢式非同步斷言。包含專案建立、[Test] 屬性、生命週期管理、並行控制與 xUnit 語法對照。 Keywords: TUnit, tunit testing, source generator testing, AOT testing, 新世代測試框架, [Test], [Arguments], TUnit.Assertions, Assert.That, Before(Test), After(Test), NotInParallel, TUnit.Templates, Microsoft.Testing.Platform, TUnit vs xUnit, 並行執行

Why use Dotnet Testing Advanced Tunit Fundamentals on TypingMind?

Because you install it once and use it with any model. Dotnet Testing Advanced Tunit Fundamentals 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 Advanced Tunit Fundamentals 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-advanced-tunit-fundamentals. 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 Advanced Tunit Fundamentals?

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 Advanced Tunit Fundamentals?

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

Is the Dotnet Testing Advanced Tunit Fundamentals 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 👇