Comprehensive guide to code coverage analysis. Use when you need to analyze code coverage, generate coverage reports, or configure CI/CD coverage checks. Covers Coverlet configuration, report generation, interpreting metrics, and integration with cyclomatic complexity. Includes Fine Code Coverage, VS Code built-in tools, and best practices. Keywords: code coverage, coverage analysis, coverage report, Coverlet, Fine Code Coverage, dotnet-coverage, ReportGenerator, line coverage, branch coverage, cyclomatic complexity, runsettings, cobertura
程式碼覆蓋率 (Code Coverage) 是一種測量指標,用來統計測試執行時實際執行了多少程式碼。
實際價值: 找出測試盲點、評估測試完整性、輔助重構決策、增加測試信心。
常見誤解:
| 工具 | 優點 | 適用場景 | | ---------------------------- | ------------------------------ | -------------- | | Visual Studio Enterprise | 內建整合,完整 UI | 僅 Enterprise | | Fine Code Coverage(推薦) | 免費,即時顯示,編輯器直接標示 | VS 所有版本 | | .NET CLI + Coverlet | 跨平台,CLI 自動化 | CI/CD 流程 | | VS Code 內建 | 跨平台,無需額外擴充套件 | VS Code 開發 |
True、Editor Colouring Line Highlighting: True# .NET CLI(推薦用於 CI/CD)
dotnet test --collect:"XPlat Code Coverage"
dotnet test --collect:"XPlat Code Coverage" --results-directory ./coverage
# Fine Code Coverage:在 VS 中執行測試後自動顯示
# VS Code:開啟測試總管 → 點選「執行涵蓋範圍測試」
| 方法 | 工具 | 操作步驟 |
| ----------------- | --------------------- | --------------------------------------------- |
| .NET CLI | Coverlet + CLI | 執行 dotnet test --collect:"XPlat Code Coverage" |
| Fine Code Coverage | VS 擴充套件 | 在 VS 中執行測試後自動顯示 |
| VS Code | C# Dev Kit | 開啟測試總管 → 執行涵蓋範圍測試 |
測試專案需安裝 coverlet.collector 套件,並可透過 runsettings 檔案進行進階設定(排除規則、閾值、報告格式)。
完整 csproj 配置範例請參考 references/coverlet-csproj-config.md
顏色標示: 綠色(已覆蓋)、黃色(部分覆蓋)、紅色(未覆蓋)
覆蓋率指標:
報告解讀策略: 優先處理紅色區域(完全未測試)→ 檢查黃色區域(部分分支未測試)→ 評估必要性(簡單 getter/setter 可跳過)
循環複雜度(Cyclomatic Complexity)代表程式中獨立邏輯路徑的數量,等於至少需要的測試案例數量。每個 if、for、while、case、&&、|| 都會增加複雜度。
Visual Studio 擴充套件:
完整範例與測試策略請參考 references/cyclomatic-complexity-example.md
漸進式改善流程:
優先順序:
排除不必要的程式碼: 使用 [ExcludeFromCodeCoverage] 屬性或在 runsettings 中排除。
覆蓋率分析可整合至 CI/CD Pipeline,在 GitHub Actions 中使用 dotnet test --collect:"XPlat Code Coverage" 搭配 reportgenerator 產生報告;在 Azure DevOps 中使用 DotNetCoreCLI@2 任務搭配 PublishCodeCoverageResults@1。
完整 YAML 設定範例請參閱 references/cicd-integration.md
coverlet.collector 套件coverlet.collector 套件dotnet test --collect:"XPlat Code Coverage"templates/runsettings-template.xml - 覆蓋率設定範本templates/coverage-workflow.md - 完整的工作流程說明程式碼覆蓋率是手段,不是目的。 重點在於關鍵業務邏輯是否都有測試、測試是否真正驗證了預期行為、是否能在重構時提供信心。
coverage.runsettings 設定檔,配置覆蓋率收集參數與排除規則.csproj,確保包含 coverlet.collector 套件參考unit-test-fundamentals - 單元測試基礎與 FIRST 原則xunit-project-setup - xUnit 專案設定npx skills add kevintsengtw/dotnet-测试-代码覆盖率分析下载完整 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