A specialized skill for creating and configuring xUnit test projects. Use this when you need to create a test project, set up project structure, configure NuGet packages, and organize test folders. Covers csproj settings, package management, project structure, xunit.runner.json configuration, and related topics. Keywords: xunit project, xunit setup, test project creation, test project setup, create test project, project structure, folder structure, xunit package, nuget packages, test suite, how to create test project, xunit configuration
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
建立解決方案
加入主專案
MyProject.Core加入測試專案
MyProject.Core.Tests設定專案參考
請參考同目錄下的 templates/xunit-test-project.csproj 範本檔案。
核心相依套件說明:
xunit(2.9.3+)
[Fact]、[Theory] 等測試屬性Assert 類別與斷言方法xunit.runner.visualstudio(3.0.0+)
Microsoft.NET.Test.Sdk(18.3.0+)
dotnet test 指令能夠執行測試coverlet.collector(8.0.0+)
dotnet test 整合<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整合測試:{專案名稱}.Test.Integration
MyProject.WebApi.Test.Integration這種命名的優勢:
清晰度:一眼就能分辨測試類型
執行策略:可以在 CI/CD 中分階段執行
# 快速回饋:只執行單元測試
dotnet test --filter "FullyQualifiedName~.Test.Unit"
# 完整驗證:執行整合測試
dotnet test --filter "FullyQualifiedName~.Test.Integration"
相依性管理:整合測試可以有不同的套件相依(如 Testcontainers)
團隊協作:新成員能快速理解專案結構
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 天挑戰」系列文章:
Day 02 - xUnit 框架深度解析
Day 03 - xUnit 進階功能與測試資料管理
unit-test-fundamentals - 單元測試基礎與 FIRST 原則test-naming-conventions - 測試命名規範code-coverage-analysis - 程式碼覆蓋率分析npx skills add kevintsengtw/dotnet 测试 xUnit 项目设置下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer