dotnet-testing-xunit-project-setup — independently scanned and version-tracked by SaferSkills.
SaferSkills independently audited dotnet-testing-xunit-project-setup (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.
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#### 步驟 1:建立解決方案與專案
# 建立解決方案
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#### 步驟 2:安裝程式碼覆蓋率工具
# 切換到測試專案目錄
cd tests/MyProject.Core.Tests
# 安裝 coverlet.collector(用於收集程式碼覆蓋率)
dotnet add package coverlet.collectorMyProject.CoreMyProject.Core.Tests請參考同目錄下的 templates/xunit-test-project.csproj 範本檔案。
核心相依套件說明:
[Fact]、[Theory] 等測試屬性Assert 類別與斷言方法dotnet test 指令能夠執行測試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#### VS Code(需安裝 C# Dev Kit)
#### Visual Studio
#### JetBrains Rider
測試專案 → 主專案 (正確)
主專案 → 測試專案 (錯誤)測試專案應該參考主專案,但主專案絕對不應參考測試專案。
# 讓測試專案參考主專案
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.UnitMyProject.Core.Test.Unit{專案名稱}.Test.IntegrationMyProject.WebApi.Test.Integration這種命名的優勢:
# 快速回饋:只執行單元測試
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 天挑戰」系列文章:
unit-test-fundamentals - 單元測試基礎與 FIRST 原則test-naming-conventions - 測試命名規範code-coverage-analysis - 程式碼覆蓋率分析~30 seconds. Free. No account. Every finding cites a rule and a line of evidence.