dotnet-testing-code-coverage-analysis — independently scanned and version-tracked by SaferSkills.
SaferSkills independently audited dotnet-testing-code-coverage-analysis (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.
程式碼覆蓋率 (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 專案設定~30 seconds. Free. No account. Every finding cites a rule and a line of evidence.