
Coverage Analysis -【免費下載鏈接】skillsRepository for skills to assist AI coding agents with .NET and C#項目地址: https://gitcode.com/GitHub_Trending/skills17/skillsMetricValueDateLine Coverage%Branch Coverage%Risk Hotspots(CRAP crap_threshold)Testspassed · failedSummaryMetricValueThresholdStatusLine Coverage%line_threshold%? / ?Branch Coverage%branch_threshold%? / ?Methods Analyzed——Risk Hotspots0? / ??Test ResultPassed / N tests failed—? / ??Coverage collected fromof test project(s). Outputs saved to:coverageDir/(markdown summary raw Cobertura XML).If HTML/CSV generation ran:additional reports are atcoverageDir/reports/.If any coverage provider package was added to test projects, include this note after the summary:??Coverage provider package updatescoverlet.collectoradded toKproject(s):TestProject1.csproj,TestProject2.csprojMicrosoft.Testing.Extensions.CodeCoverageadded toMproject(s):TestProject3.csprojTo revert:git checkout -- path-to-each-modified-csprojIf all test projects already had a coverage provider, omit this note. Risk Hotspots (Top by CRAP Score)Methods flagged as high-risk: complex code with low test coverage that is dangerous to change.RankMethodClassFileComplexityCoverageCRAP Score1methodclassfile%…………………CRAP ScoreComplexity2 × (1 ? Coverage)3 Complexity. Scores above crap_threshold are flagged. A score ≤ 5 is considered safe. Coverage Gaps by FileFiles below the line or branch coverage threshold, ordered by uncovered lines descending:FileLine CoverageBranch CoverageUncovered LinesPriorityfile%% HIGH / MED / LOW…………… RecommendationsWrite tests for the top risk hotspot first—methodinclasshas a CRAP score of (complexity , % coverage). Reducing it to 80% coverage would drop the score to ~ .Focus onfile— uncovered lines, below threshold.Up to 5 actionable items total, ordered by expected risk reduction. ReportsReportPathMarkdown summary (this file)coverageDir/coverage-analysis.mdRaw Cobertura XMLcoberturaXmlPathsUsedForAnalysisHTML (browsable)coverageDir/reports/index.htmlorNot generated (optional — request HTML reports to enable)Text summarycoverageDir/reports/Summary.txtorNot generatedGitHub markdowncoverageDir/reports/SummaryGithub.mdorNot generatedCSV datacoverageDir/reports/Summary.csvorNot generated### 3.1 頭部指標表一分鐘速覽 H1 標題為 Coverage Analysis - ProjectName緊隨其后的表格給出五個核心指標 - **Date**報告生成日期YYYY-MM-DD用于追溯報告時效 - **Line Coverage**行覆蓋率N%來自 Cobertura 聚合數(shù)據(jù) - **Branch Coverage**分支覆蓋率N% - **Risk Hotspots**CRAP 分數(shù)超過 crap_threshold 的方法數(shù)量默認閾值見下文 - **Tests**N passed · N failed注意必須使用 · 作為分隔符。 其中行/分支覆蓋率應當取自 Cobertura 的聚合計數(shù)。這一點在 [SKILL.md](https://link.gitcode.com/i/4bee565a9fe3b035cb65b4e936eb3e70) 的 Interpretation and arithmetic invariants 中有明確規(guī)定**總體總計必須從 Cobertura 的 covered/valid 行數(shù)推導**而不是簡單地把各方法數(shù)字相加——方法行區(qū)間可能重疊或遺漏類級行。 ### 3.2 Summary 表閾值對照與狀態(tài)判定 Summary 表把關鍵指標與閾值并排展示并給出狀態(tài)符號 - **Line Coverage** 與 **Branch Coverage** 分別對照 line_threshold 與 branch_threshold達標為 ?否則 ? - **Methods Analyzed** 為分析方法總數(shù)無閾值 - **Risk Hotspots** 的閾值為 0即出現(xiàn)任何熱點即為 ?? - **Test Result** 顯示 Passed / N tests failed全部通過為 ?否則 ??。 閾值的默認值定義在 SKILL.md 的 Inputs 表中 | 輸入 | 必填 | 默認值 | 說明 | |---|---|---|---| | 項目/解決方案路徑 | 否 | 當前目錄 | .NET solution 或 project 路徑 | | 行覆蓋率閾值 | 否 | 80% | 可接受的最低行覆蓋率 | | 分支覆蓋率閾值 | 否 | 70% | 可接受的最低分支覆蓋率 | | 既有 Cobertura 路徑 | 否 | 僅未提供時自動發(fā)現(xiàn) | 優(yōu)先輸入可用時絕不重跑測試 | | CRAP 閾值 | 否 | 30 | 僅顯式風險/CRAP 請求時使用 | | 熱點數(shù)量 | 否 | 3 | 僅顯式風險請求除非用戶要求更多上限 10 | 也就是說line_threshold 默認填入 80branch_threshold 默認填入 70crap_threshold 默認填入 30。 ### 3.3 引用塊注釋數(shù)據(jù)來源與產(chǎn)物位置 Summary 之后的引用塊聲明三件事 - **Coverage collected from N of M test project(s)**說明覆蓋率數(shù)據(jù)來自多少個測試項目中的多少個——多測試項目場景下需要如實填寫 - **Outputs saved to coverageDir/**聲明產(chǎn)物位置markdown 摘要 原始 Cobertura XML - **If HTML/CSV generation ran: additional reports are at coverageDir/reports/**只有真正運行了 HTML/CSV 生成時才寫這一條。 ### 3.4 覆蓋率提供程序包更新說明條件性區(qū)塊 If any coverage provider package was added to test projects, include this note after the summary: 如果收集覆蓋率時向測試項目添加了覆蓋率提供程序包必須在 Summary 之后附加這條說明 - coverlet.collector 被添加到 K 個項目列出項目文件路徑 - Microsoft.Testing.Extensions.CodeCoverage 被添加到 M 個項目列出項目文件路徑 - 給出回滾命令git checkout -- path-to-each-modified-csproj。 **如果所有測試項目原本就已有覆蓋率提供程序則省略此區(qū)塊。** 這條規(guī)則與 SKILL.md 的 Collection path 一致任何為收集覆蓋率而做的附帶測試項目改動歸 run-tests 所有必須在最終分析中包含該改動及其回滾命令。 ### 3.5 Risk Hotspots按 CRAP 分數(shù)排序的熱點表 這一節(jié)回答哪些代碼最危險、最不該在沒有測試的情況下改動 | 列 | 含義 | |---|---| | Rank | 風險排名Top N默認 3用戶要求可到 10 | | Method / Class | 方法名與所屬類 | | File | 源文件路徑 | | Complexity | 圈復雜度Cobertura 的 complexity 屬性 | | Coverage | 方法級行覆蓋率 | | CRAP Score | 綜合風險分加粗顯示 | 模板同時給出 CRAP 公式與判定規(guī)則 **CRAP Score** Complexity2 × (1 ? Coverage)3 Complexity. Scores above crap_threshold are flagged. A score ≤ 5 is considered safe. 公式源自 Alberto Savoia 的原始 CRAP 定義[Compute-CrapScores.ps1](https://link.gitcode.com/i/6b96f20408f03f33966c45c5cd65978c) 中的實現(xiàn)注釋解釋了其設計直覺(1 ? Coverage) 的**三次方**對低覆蓋率進行劇烈懲罰——覆蓋率 0% 時風險乘數(shù)為 1.050% 時驟降至 0.125。由此可以推導出兩個關鍵結論 - 一個 100% 覆蓋的方法其 CRAP 恰好等于它的復雜度 - 復雜度高且覆蓋率低的方法分數(shù)會以平方 × 立方的速度膨脹成為最危險的重構對象。 在 [guidelines.md](https://link.gitcode.com/i/31ef9df9f7de985e436fef4f2085fbfc) 中還補充了輸出紀律**默認只展示 Top 3 真實熱點排除完全覆蓋的低風險方法其余超標方法只報告數(shù)量**用戶指定時可到 10 行除非用戶明確要求更大的數(shù)量。 ### 3.6 Coverage Gaps by File按未覆蓋行數(shù)排序的缺口表 Files below the line or branch coverage threshold, ordered by uncovered lines descending: 列出低于行或分支覆蓋率閾值的文件按**未覆蓋行數(shù)降序**排列每行包含文件、行覆蓋率、分支覆蓋率、未覆蓋行數(shù)與優(yōu)先級。 優(yōu)先級的判定規(guī)則定義在 [guidelines.md](https://link.gitcode.com/i/31ef9df9f7de985e436fef4f2085fbfc) - ** HIGH**CRAP 與覆蓋率**雙雙**超過各自的風險閾值 - ** MED**CRAP 或覆蓋率**任一**超過閾值 - ** LOW**低于覆蓋率閾值但復雜度 ≤ 2。 同時指南強調(diào)應優(yōu)先關注**復雜的未覆蓋關鍵路徑**如認證、支付、數(shù)據(jù)訪問、錯誤處理而將平凡 getter、生成代碼、遷移文件和配置膠水代碼降權處理。 ### 3.7 Recommendations按預期風險降低排序的行動項 模板預設了前兩條建議的寫法第三條起為占位說明**總計最多 5 條行動項按預期風險降低程度排序** 1. **優(yōu)先為排名第一的熱點寫測試**——給出 method 在 class 中的 CRAP 分數(shù)復雜度 N、覆蓋率 N%并演示補到 80% 覆蓋率后分數(shù)會降至 ~projected的推算。注意 guidelines.md 的告誡**不得在沒有說明假設的情況下從任意目標反推 CRAP 降幅**——必須用聲明的目標覆蓋率重新計算 2. **聚焦某個缺口文件**——N 行未覆蓋、低于閾值附一句簡要理由 3. **其余行動項**——按風險降低效果排序補足。 ### 3.8 Reports產(chǎn)物路徑清單 最后一張表列出六類產(chǎn)物及其路徑 | 報告 | 路徑 | |---|---| | Markdown 摘要本文件 | coverageDir/coverage-analysis.md | | 原始 Cobertura XML | coberturaXmlPathsUsedForAnalysis | | HTML可瀏覽 | coverageDir/reports/index.html 或 Not generated | | 文本摘要 | coverageDir/reports/Summary.txt 或 Not generated | | GitHub Markdown | coverageDir/reports/SummaryGithub.md 或 Not generated | | CSV 數(shù)據(jù) | coverageDir/reports/Summary.csv 或 Not generated | 模板末尾對這張表給出了兩條硬性規(guī)則 If ReportGenerator has not run, mark the HTML/Text/GitHub-markdown/CSV rows as Not generated (optional — request HTML reports to enable). Do not invent paths for files that have not been produced. For **Raw Cobertura XML**, list the actual XML file path(s) used in analysis (for from-scratch runs this is typically under coverageDir/raw/; for existing-data runs this may be under TestResults/ or another user-supplied location). 翻譯成可執(zhí)行紀律 - **絕不編造產(chǎn)物路徑**ReportGenerator 沒跑就如實寫 Not generated (optional — request HTML reports to enable) - **Raw Cobertura XML 必須列實際用于分析的 XML 路徑**從零采集from-scratch時通常在 coverageDir/raw/ 下使用既有數(shù)據(jù)existing-data時可能在 TestResults/ 下或用戶提供的其他位置。 這與 SKILL.md 的 Report only artifacts that exist只報告真實存在的產(chǎn)物、以及 Common Pitfalls 中的 No Cobertura from collection — report the collection failure; do not invent substitute coverage 一脈相承。 ## 四、報告數(shù)據(jù)從哪來兩個 PowerShell 腳本 全量報告中的熱點與缺口數(shù)據(jù)來自 skill 自帶的兩個腳本調(diào)用方式見 [guidelines.md](https://link.gitcode.com/i/31ef9df9f7de985e436fef4f2085fbfc) powershell skill-directory/scripts/Compute-CrapScores.ps1 -CoberturaPath (all Cobertura paths) -CrapThreshold crap_threshold -TopN top_n skill-directory/scripts/Extract-MethodCoverage.ps1 -CoberturaPath (all Cobertura paths) -CoverageThreshold line_threshold -BranchThreshold branch_threshold -Filter below-threshold4.1 Compute-CrapScores.ps1聚合覆蓋率并計算 CRAPCompute-CrapScores.ps1 的參數(shù)為-CoberturaPath必填string[]一個或多個 Cobertura XML 路徑-CrapThreshold默認 30超標閾值-TopN默認 10熱點輸出數(shù)量。其核心邏輯以Class|Method|Signature|File為穩(wěn)定鍵跨文件合并方法行命中數(shù)累加——任何一份覆蓋率文件覆蓋了該行即視為已覆蓋優(yōu)先使用 Cobertura 頂層的lines-covered/lines-valid、branches-covered/branches-valid聚合屬性計算總體行/分支覆蓋率缺失時回退到line-rate/branch-rate的平均值再回退到合并后的方法級行統(tǒng)計對每個方法計算complexity2 × (1 ? coverage)3 complexity并保留兩位小數(shù)輸出OVERALL_LINE_COVERAGE、OVERALL_BRANCH_COVERAGE、TOTAL_METHODS、FLAGGED_METHODS以及HOTSPOTS按 CRAP 降序取 Top N 的 JSON。這些輸出正好對應報告中的頭部指標、Methods Analyzed 與 Risk Hotspots 數(shù)量、以及熱點表的內(nèi)容。4.2 Extract-MethodCoverage.ps1提取方法級覆蓋率缺口Extract-MethodCoverage.ps1 的參數(shù)為-CoberturaPath必填string[]Cobertura XML 路徑支持多測試項目合并-CoverageThreshold默認 80行覆蓋率閾值-BranchThreshold默認 70分支覆蓋率閾值-Filteruncovered/below-threshold/all默認all。它的特色在于分支數(shù)據(jù)的合并解析行元素中branchtrue的condition-coverage屬性形如50% (1/2)跨文件合并時已覆蓋分支數(shù)累加但封頂于總數(shù)。輸出為 JSON 數(shù)組包含類、方法、簽名、文件、復雜度、行/分支覆蓋率、覆蓋/總行數(shù)、未覆蓋行數(shù)、覆蓋/總分支數(shù)排序規(guī)則是未覆蓋行數(shù)降序、行覆蓋率升序——與報告缺口表的排序要求完全一致。腳本還以 exit code 0/2 區(qū)分成功與文件缺失/解析失敗。值得注意SKILL.md 規(guī)定只有當用戶顯式請求風險熱點 / CRAP / 復雜度加權優(yōu)先級 / 重構安全時才運行這兩個腳本并計算 CRAP普通的摘錄解釋、平臺期診斷或目標算術一律不計算 CRAP。五、HTML/CSV 可選報告鏈路ReportGenerator如果用戶同時要求 HTML/CSV 報告需要走 report-generation.md 定義的流程前提是直接答案已經(jīng)交付、且用戶明確請求了報告文件或 CI 產(chǎn)物。該文檔首先檢查reportgenerator是否可用不可用時在當前覆蓋率目錄下安裝全局工具dotnet tool install dotnet-reportgenerator-globaltool --tool-path COVERAGE_DIR/.tools隨后執(zhí)行一次生成命令同時產(chǎn)出 HTML、文本摘要、GitHub Markdown 摘要與 CSV 四類報告reportgenerator -reports:semicolon-separated COBERTURA paths -targetdir:$reportsDir -reporttypes:Html;TextSummary;MarkdownSummaryGithub;CsvSummary -title:Coverage Report -tag:coverage-analysis-skill【免費下載鏈接】skillsRepository for skills to assist AI coding agents with .NET and C#項目地址: https://gitcode.com/GitHub_Trending/skills17/skills創(chuàng)作聲明:本文部分內(nèi)容由AI輔助生成(AIGC),僅供參考