Summary
- TUnit 新世代測試框架入門完整指南。當需要使用 TUnit 建立測試專案或從 xUnit 遷移至 TUnit 時使用。涵蓋 Source Generator 驅動測試發現、AOT 編譯支援、流暢式非同步斷言。包含專案建立、[Test] 屬性、生命週期管理、並行控制與 xUnit 語法對照。
- Make sure…
kevintsengtw/dotnet-testing-agent-skills
TUnit 新世代測試框架? Make sure to use this skill whenever the user mentions TUnit, Source Generator testing, AOT test framework, TUnit vs xUnit, or migrating to TUnit, even if they don't explicitly ask for TUnit fundamentals.
npx skills add kevintsengtw/dotnet-testing-agent-skills --skill dotnet-testing-advanced-tunit-fundamentals
Related neighbors and high-traction skills in the same topics — useful to compare before installing.
Browser automation CLI for AI agents. Use when the user needs to interact with websites, includ…
810.4K installsConfigure Azure API Management as an AI Gateway for AI models, MCP tools, and agents. WHEN: sem…
566.3K installsAzure VM/VMSS router. WHEN: create / provision / deploy / spin-up VM, recommend VM size, compar…
510K installsPostgres best practices maintained by Supabase, for Postgres running anywhere. Load this skill …
391.6K installsPrisma ORM CLI commands reference covering init, generate, migrate, db, dev, complete, studio, …
267K installsUse when doing ANY task involving Supabase. Triggers: Supabase products (Database, Auth, Edge F…
263K installsOther skills from kevintsengtw/dotnet-testing-agent-skills · top by installs.
npx skills add kevintsengtw/dotnet-testing-agent-skills
Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.
Alternate registries and mirrors of this skill.
npx skills add smithery/kevintsengtw --skill dotnet-testing-advanced-tunit-fundamentals
main
Files included with this skill beyond the listing page.
SKILL.md
7,117 B
SUMMARY.md
832 B
TUnit 與傳統測試框架最大的差異在於使用 Source Generator 在編譯時期完成測試發現,避免反射成本、完全支援 Native AOT、更快的啟動時間。
// TUnit 在編譯時期就透過 Source Generator 產生測試註冊程式碼
public class ModernTests
{
[Test] // 編譯時期就被處理和最佳化
public async Task TestMethod()
{
await Assert.That(true).IsTrue();
}
}
傳統 JIT:C# 原始碼 → IL 中間碼 → 執行時期 JIT 編譯 → 機器碼 → 執行
AOT: C# 原始碼 → 編譯時期直接產生 → 機器碼 → 直接執行
AOT 優勢:超快啟動時間、更小的記憶體占用、可預測的效能、更適合容器化部署。大型專案可達 10-30 倍啟動時間改善。
TUnit 建構在微軟最新的 Microsoft.Testing.Platform 之上,而非傳統的 VSTest 平台。
重要注意事項: TUnit 專案不需要也不應該安裝 Microsoft.NET.Test.Sdk 套件。
TUnit 將並行執行設為預設,需要時可用 [NotInParallel("GroupName")] 控制特定測試群組依序執行。
支援手動建立(console 模板 + TUnit 套件)或使用 TUnit Template(dotnet new tunit,推薦)。專案需設定 csproj(TUnit 套件、IsTestProject)與 GlobalUsings.cs。所有測試方法必須是非同步的(async Task)。
完整專案建立步驟與 csproj 範例請參閱 [references/project-setup.md](references/project-setup.md)
TUnit 統一使用 [Test] 屬性,不像 xUnit 區分 [Fact] 和 [Theory]:
[Test]
public async Task Add_輸入1和2_應回傳3()
{
var calculator = new Calculator();
var result = calculator.Add(1, 2);
await Assert.That(result).IsEqualTo(3);
}
[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);
}
TUnit 採用流暢式(Fluent)斷言設計,所有斷言都是非同步的。支援相等性、布林值、數值比較、字串、集合、例外等多種斷言,並可透過 And / Or 組合條件。
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](references/tunit-assertions-detail.md)
TUnit 支援建構式 / Dispose 模式,以及 [Before(Test)]、[Before(Class)]、[After(Test)]、[After(Class)] 等屬性。
執行順序:Before(Class) → 建構式 → Before(Test) → 測試方法 → After(Test) → Dispose → After(Class)
完整生命週期範例與屬性對照表請參閱 [references/lifecycle-management.md](references/lifecycle-management.md)
TUnit 預設並行執行所有測試,使用 [NotInParallel] 控制特定群組依序執行。提供完整的 xUnit → TUnit 語法對照表、遷移範例、CLI 執行指令與 IDE 整合設定。
完整並行控制、語法對照與遷移範例請參閱 [references/xunit-migration.md](references/xunit-migration.md)
| 場景 | xUnit | TUnit | TUnit AOT | 效能提升 |
|---|---|---|---|---|
| 簡單測試執行 | 1,400ms | 1,000ms | 60ms | 23x (AOT) |
| 非同步測試 | 1,400ms | 930ms | 26ms | 54x (AOT) |
| 並行測試 | 1,425ms | 999ms | 54ms | 26x (AOT) |
Microsoft.NET.Test.Sdk,TUnit 使用新的測試平台await,測試方法必須是 async Task本技能內容提煉自「老派軟體工程師的測試修練 - 30 天挑戰」系列文章:
- 鐵人賽文章:https://ithelp.ithome.com.tw/articles/10377828 - 範例程式碼:https://github.com/kevintsengtw/30DaysinTesting_Samples/tree/main/day28
dotnet-testing-advanced-tunit-advanced - TUnit 進階功能dotnet-testing-advanced-xunit-upgrade-guide - xUnit 升級指南dotnet-testing-xunit-project-setup - xUnit 專案設定(比較用)