Summary
- xUnit 測試專案建立與設定的專門技能。當需要建立測試專案、設定專案結構、配置 NuGet 套件、組織測試資料夾時使用。涵蓋 csproj 設定、套件管理、專案結構、xunit.runner.json 配置等。
- Make sure to use this skill whenever the user…
kevintsengtw/dotnet-testing-agent-skills
xUnit 測試專案建立與設定的專門技能。當需要建立測試專案、設定專案結構、? Make sure to use this skill whenever the user mentions creating a test project, xUnit setup, project structure for tests, NuGet test packages, or csproj configuration for testing, even if they don't explicitly ask for project setup guidance.
npx skills add kevintsengtw/dotnet-testing-agent-skills --skill dotnet-testing-xunit-project-setup
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-xunit-project-setup
main
Files included with this skill beyond the listing page.
SKILL.md
12,665 B
SUMMARY.md
764 B
MyProject/
├── src/ # 主程式碼目錄
│ └── MyProject.Core/
│ ├── MyProject.Core.csproj
│ ├── Calculator.cs
│ ├── Services/
│ └── Models/
├── tests/ # 測試程式碼目錄
│ └── MyProject.Core.Tests/
│ ├── MyProject.Core.Tests.csproj
│ ├── CalculatorTests.cs
│ ├── Services/
│ └── Models/
└── MyProject.sln
結構原則:
{主專案名稱}.Tests# 建立解決方案
dotnet new sln -n MyProject
# 建立主專案(類別庫)
dotnet new classlib -n MyProject.Core -o src/MyProject.Core
# 建立測試專案(xUnit 範本)
dotnet new xunit -n MyProject.Core.Tests -o tests/MyProject.Core.Tests
# 將專案加入解決方案
dotnet sln add src/MyProject.Core/MyProject.Core.csproj
dotnet sln add tests/MyProject.Core.Tests/MyProject.Core.Tests.csproj
# 建立專案參考(測試專案參考主專案)
dotnet add tests/MyProject.Core.Tests/MyProject.Core.Tests.csproj reference src/MyProject.Core/MyProject.Core.csproj
# 切換到測試專案目錄
cd tests/MyProject.Core.Tests
# 安裝 coverlet.collector(用於收集程式碼覆蓋率)
dotnet add package coverlet.collector
- File → New → Project - 選擇「Blank Solution」 - 命名為專案名稱
- 右鍵解決方案 → Add → New Project - 選擇「Class Library」 - 命名為 MyProject.Core
- 右鍵解決方案 → Add → New Project - 搜尋並選擇「xUnit Test Project」 - 命名為 MyProject.Core.Tests
- 右鍵測試專案 → Add → Project Reference - 勾選主專案
請參考同目錄下的 templates/xunit-test-project.csproj 範本檔案。
核心相依套件說明:
- xUnit 測試框架的核心套件 - 提供 [Fact]、[Theory] 等測試屬性 - 包含 Assert 類別與斷言方法
- Visual Studio Test Explorer 整合 - 讓測試能在 VS Code、Visual Studio、Rider 中被探索與執行 - 支援測試結果的即時顯示
- .NET 測試平台的 SDK - 讓 dotnet test 指令能夠執行測試 - 支援測試結果報告與測試探索
- 程式碼覆蓋率收集工具 - 與 dotnet test 整合 - 產生覆蓋率報告(支援 Cobertura、OpenCover 等格式)
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
設定說明:
IsPackable=false:測試專案不應被打包成 NuGet 套件IsTestProject=true:明確標記為測試專案,讓工具識別Nullable=enable:啟用可為 Null 的參考型別檢查namespace MyProject.Core.Tests;
public class CalculatorTests
{
private readonly Calculator _calculator;
// 建構函式:每個測試執行前都會被呼叫
public CalculatorTests()
{
_calculator = new Calculator();
}
}
xUnit 的測試隔離機制:
IDisposable,在每個測試方法執行後被呼叫執行順序範例:
執行 Test1:
→ 建構函式 → Test1 方法 → Dispose()
執行 Test2:
→ 建構函式 → Test2 方法 → Dispose()
這確保了 測試隔離,符合 FIRST 原則的 I (Independent)。
# 建置專案
dotnet build
# 執行所有測試
dotnet test
# 執行測試並收集程式碼覆蓋率
dotnet test --collect:"XPlat Code Coverage"
# 執行特定測試專案
dotnet test tests/MyProject.Core.Tests/MyProject.Core.Tests.csproj
# 執行測試並產生詳細輸出
dotnet test --verbosity detailed
測試專案 → 主專案 (正確)
主專案 → 測試專案 (錯誤)
測試專案應該參考主專案,但主專案絕對不應參考測試專案。
# 讓測試專案參考主專案
dotnet add tests/MyProject.Core.Tests/MyProject.Core.Tests.csproj reference src/MyProject.Core/MyProject.Core.csproj
在 csproj 中會產生:
<ItemGroup>
<ProjectReference Include="..\..\src\MyProject.Core\MyProject.Core.csproj" />
</ItemGroup>
當專案變大時,可能需要多個測試專案:
MyProject/
├── src/
│ ├── MyProject.Core/
│ ├── MyProject.Web/
│ └── MyProject.Infrastructure/
├── tests/
│ ├── MyProject.Core.Tests/ # 單元測試
│ ├── MyProject.Web.Tests/ # Web 層測試
│ ├── MyProject.Infrastructure.Tests/ # 基礎設施測試
│ └── MyProject.Integration.Tests/ # 整合測試
└── MyProject.sln
命名慣例建議:
*.Tests - 單元測試*.Integration.Tests - 整合測試*.Acceptance.Tests - 驗收測試*.Performance.Tests - 效能測試在實際的工作專案中,建議使用更明確的命名格式來區分測試類型:
推薦的命名格式:
MyProject/
├── src/
│ ├── MyProject.Core/
│ └── MyProject.WebApi/
├── tests/
│ ├── MyProject.Core.Test.Unit/ # 單元測試(明確標示)
│ ├── MyProject.WebApi.Test.Unit/ # WebApi 單元測試
│ └── MyProject.WebApi.Test.Integration/ # WebApi 整合測試
└── MyProject.sln
命名規則:
{專案名稱}.Test.Unit- 範例:MyProject.Core.Test.Unit - 特性:不依賴外部資源(資料庫、API、檔案系統等) - 執行速度:快速(毫秒級)
{專案名稱}.Test.Integration- 範例:MyProject.WebApi.Test.Integration - 特性:測試多個元件的整合,可能依賴外部資源 - 執行速度:較慢(秒級)
這種命名的優勢:
```powershell # 快速回饋:只執行單元測試 dotnet test --filter "FullyQualifiedName~.Test.Unit"
# 完整驗證:執行整合測試 dotnet test --filter "FullyQualifiedName~.Test.Integration" ```
CLI 建立範例:
# 建立單元測試專案
dotnet new xunit -n MyProject.Core.Test.Unit -o tests/MyProject.Core.Test.Unit
dotnet add tests/MyProject.Core.Test.Unit reference src/MyProject.Core
# 建立整合測試專案
dotnet new xunit -n MyProject.WebApi.Test.Integration -o tests/MyProject.WebApi.Test.Integration
dotnet add tests/MyProject.WebApi.Test.Integration reference src/MyProject.WebApi
提示:雖然本範例中為了簡化說明使用
.Tests格式,但在實際專案中強烈建議使用.Test.Unit和.Test.Integration這種更明確的格式。
檢查清單:
xunit.runner.visualstudio 套件Microsoft.NET.Test.Sdk 套件dotnet build 重新建置解決方案:
bin/ 和 obj/ 資料夾後重新建置.csproj 中的 IsTestProject 屬性是否為 true在主專案的 .csproj 或 AssemblyInfo.cs 中加入:
[assembly: InternalsVisibleTo("MyProject.Core.Tests")]
或在 csproj 中:
<ItemGroup>
<InternalsVisibleTo Include="MyProject.Core.Tests" />
</ItemGroup>
請參考同目錄下的範本檔案以快速建立專案:
templates/project-structure.md - 完整的專案結構範例templates/xunit-test-project.csproj - xUnit 測試專案的 csproj 範本建立 xUnit 測試專案時,請確認以下項目:
{主專案名稱}.Teststests/ 目錄下xunit、xunit.runner.visualstudio、Microsoft.NET.Test.Sdk 套件coverlet.collector 用於程式碼覆蓋率IsPackable 設為 falseIsTestProject 設為 truedotnet test 成功本技能內容提煉自「老派軟體工程師的測試修練 - 30 天挑戰」系列文章:
- 鐵人賽文章:https://ithelp.ithome.com.tw/articles/10373952 - 範例程式碼:https://github.com/kevintsengtw/30DaysinTesting_Samples/tree/main/day02
- 鐵人賽文章:https://ithelp.ithome.com.tw/articles/10374064 - 範例程式碼:https://github.com/kevintsengtw/30DaysinTesting_Samples/tree/main/day03
unit-test-fundamentals - 單元測試基礎與 FIRST 原則test-naming-conventions - 測試命名規範code-coverage-analysis - 程式碼覆蓋率分析