dotnet-testing-advanced-tunit-fundamentals — independently scanned and version-tracked by SaferSkills.
SaferSkills independently audited dotnet-testing-advanced-tunit-fundamentals (Agent Skill) and scored it 100/100 (green). The audit ran 55 deterministic rules across Security, Supply Chain, Maintenance, Transparency, and Community; it found 0 high-severity and 0 lower-severity findings. The full rule-by-rule trace and per-finding evidence are below. Free, methodology-open.
Findings & checks · 0 flagged
Every scanned point with the score it earned and what moved between them.
First recorded scan — no prior version to compare against.
The primary manifest — the file an agent reads to learn what this artifact does.
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
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
TUnit 支援建構式 / Dispose 模式,以及 [Before(Test)]、[Before(Class)]、[After(Test)]、[After(Class)] 等屬性。
執行順序:Before(Class) → 建構式 → Before(Test) → 測試方法 → After(Test) → Dispose → After(Class)完整生命週期範例與屬性對照表請參閱 references/lifecycle-management.md
TUnit 預設並行執行所有測試,使用 [NotInParallel] 控制特定群組依序執行。提供完整的 xUnit → TUnit 語法對照表、遷移範例、CLI 執行指令與 IDE 整合設定。
完整並行控制、語法對照與遷移範例請參閱 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 天挑戰」系列文章:
dotnet-testing-advanced-tunit-advanced - TUnit 進階功能dotnet-testing-advanced-xunit-upgrade-guide - xUnit 升級指南dotnet-testing-xunit-project-setup - xUnit 專案設定(比較用)~30 seconds. Free. No account. Every finding cites a rule and a line of evidence.