Agent Skillsdotnet/skills › crap-score

crap-score

GitHub

计算 .NET 代码的 CRAP 分数,结合圈复杂度和覆盖率评估变更风险。用于识别高风险方法、优先测试目标及评估测试质量,适用于特定方法或类的分析。

plugins/dotnet-test/skills/crap-score/SKILL.md dotnet/skills

Trigger Scenarios

用户询问特定方法或类的 CRAP 分数 用户希望根据复杂度和覆盖率评估测试优先级

Install

npx skills add dotnet/skills --skill crap-score -g -y
More Options

Non-standard path

npx skills add https://github.com/dotnet/skills/tree/main/plugins/dotnet-test/skills/crap-score -g -y

Use without installing

npx skills use dotnet/skills@crap-score

指定 Agent (Claude Code)

npx skills add dotnet/skills --skill crap-score -a claude-code -g -y

安装 repo 全部 skill

npx skills add dotnet/skills --all -g -y

预览 repo 内 skill

npx skills add dotnet/skills --list

SKILL.md

Frontmatter
{
    "name": "crap-score",
    "license": "MIT",
    "description": "Calculates CRAP (Change Risk Anti-Patterns) for a named .NET method, class, or file. USE FOR: explicit CRAP calculation or coverage-and-complexity risk within that named target, including which tests to prioritize. DO NOT USE FOR: project-wide coverage\/CRAP, plateaus, or project-wide blockers\/priorities (coverage-analysis); behavioral\/pseudo-mutation gaps (test-gap-analysis); writing tests; test runs without CRAP context.\n"
}

CRAP Score Analysis

Calculate CRAP (Change Risk Anti-Patterns) scores for .NET methods to identify code that is both complex and undertested.

Background

The CRAP score combines cyclomatic complexity and code coverage into a single metric:

$$\text{CRAP}(m) = \text{comp}(m)^2 \times (1 - \text{cov}(m))^3 + \text{comp}(m)$$

Where:

  • $\text{comp}(m)$ = cyclomatic complexity of method $m$
  • $\text{cov}(m)$ = code coverage ratio (0.0 to 1.0) of method $m$
CRAP Score Risk Level Interpretation
< 5 Low Simple and well-tested
5-15 Moderate Acceptable for most code
15-30 High Needs more tests or simplification
> 30 Critical Refactor and add coverage urgently

A method with 100% coverage has CRAP = complexity (the minimum). A method with 0% coverage has CRAP = complexity^2 + complexity.

When to Use

  • User wants to assess which methods are risky due to low coverage and high complexity
  • User asks for CRAP score of specific methods, classes, or files
  • User wants to prioritize what to test next within a named method, class, or file based on coverage-and-complexity risk
  • User wants to evaluate test quality beyond simple coverage percentages

When Not to Use

  • User just wants to run tests (use run-tests skill)
  • User wants to write new tests (use code-testing-agent)
  • User only wants a coverage percentage without complexity analysis
  • User wants project-wide coverage/CRAP analysis or priorities (use coverage-analysis)

Inputs

Input Required Description
Target scope Yes Method name, class name, or file path to analyze
Test project path No Path to the test project. Defaults to discovering test projects in the solution.
Source project path No Path to the source project under analysis

Workflow

Step 1: Collect code coverage data

If no coverage data exists yet, classify the test project first. For SDK-style projects, run dotnet test with coverage collection. For classic non-SDK projects (ToolsVersion, explicit compile items, or packages.config), use only a repository-provided coverage command that emits Cobertura. If none exists, ask for Cobertura XML and stop; do not migrate the project or inject an SDK-style coverage package. CRAP scores always require real coverage data.

Check the test project's .csproj for the coverage package, then run the appropriate command:

Coverage Package Command Output Location
coverlet.collector dotnet test --collect:"XPlat Code Coverage" --results-directory ./TestResults Typically under TestResults/<guid>/coverage.cobertura.xml. Search recursively under the results directory (for example, TestResults/**/coverage.cobertura.xml) or use any explicit coverage path the user provides.
Microsoft.Testing.Extensions.CodeCoverage (.NET 9) dotnet test -- --coverage --coverage-output-format cobertura --coverage-output ./TestResults --coverage-output path
Microsoft.Testing.Extensions.CodeCoverage (.NET 10+) dotnet test --coverage --coverage-output-format cobertura --coverage-output ./TestResults --coverage-output path

Never estimate coverage

Guessed coverage produces wrong CRAP scores, which is worse than no answer. For a classic project with no repository coverage command or existing report, stop here and request Cobertura; do not use any collection fallback below.

For SDK-style projects, if the first command yields no Cobertura XML, work down this collection list before giving up:

  1. For SDK-style projects only, add a provider if none is referenced: dotnet add <test.csproj> package coverlet.collector, then re-run. Never use this fallback for packages.config or classic non-SDK projects.
  2. Use the standalone collector, which works even when the test host or a shared assembly blocks the in-proc collector: dotnet tool install --global dotnet-coverage then dotnet-coverage collect -f cobertura -o coverage.cobertura.xml "dotnet test <test.csproj>".

For any project type, if a real binary .coverage report already exists, convert or summarize that existing data with ReportGenerator:

  1. Convert the existing report: dotnet tool install --global dotnet-reportgenerator-globaltool then reportgenerator -reports:<file> -targetdir:cov -reporttypes:Cobertura.
  2. Tests fail but still run? Coverage is collected from the tests that executed — continue with that data and note the failures.

If every path fails, report that coverage could not be collected, show the commands you tried and their errors, and stop. Report complexity on its own if useful, but never publish a CRAP number derived from an assumed coverage percentage.

Step 2: Compute cyclomatic complexity

Analyze the target source files to determine cyclomatic complexity per method. Count the following decision points (each adds 1 to the base complexity of 1):

Construct Example
if if (x > 0)
else if else if (y < 0)
case (each) case 1:
for for (int i = 0; ...)
foreach foreach (var item in list)
while while (running)
do...while do { } while (cond)
catch (each) catch (Exception ex)
&& if (a && b)
|| (OR) if (a || b)
?? value ?? fallback
?. obj?.Method()
? : (ternary) x > 0 ? a : b
Pattern match arm x is > 0 and < 10

Base complexity is 1 for every method. Each decision point adds 1.

When analyzing, read the source file and count these constructs per method. Report the breakdown.

Step 3: Extract per-method coverage from Cobertura XML

Parse the Cobertura XML to find each method's line-rate attribute under the target <class> element. If line-rate is not available at method level, compute it from the <lines> elements:

$$\text{cov}(m) = \frac{\text{lines with hits} > 0}{\text{total lines}}$$

Method names in Cobertura may differ from source (async methods, lambdas). Match by line ranges when names don't align.

Step 4: Calculate CRAP scores

For each method in scope, apply the formula:

$$\text{CRAP}(m) = \text{comp}(m)^2 \times (1 - \text{cov}(m))^3 + \text{comp}(m)$$

Step 5: Present results

Present a sorted table (highest CRAP first):

| Method                          | Complexity | Coverage | CRAP Score | Risk     |
|---------------------------------|------------|----------|------------|----------|
| OrderService.ProcessOrder       | 12         | 45%      | 28.4       | High     |
| OrderService.ValidateItems      | 8          | 90%      | 8.1        | Moderate |
| OrderService.CalculateTotal     | 3          | 100%     | 3.0        | Low      |

Include:

  • Summary: total methods analyzed, how many in each risk category
  • Top offenders: methods with CRAP > 30, with specific recommendations
  • Quick wins: methods with high complexity but where small coverage improvements would drop the score significantly

Step 6: Provide actionable recommendations

For high-CRAP methods, suggest one or both:

  1. Add tests -- identify uncovered branches and suggest specific test cases
  2. Reduce complexity -- suggest extract-method refactoring for deeply nested logic

Calculate the coverage needed to bring a method below a CRAP threshold of 15:

$$\text{cov}_{\text{needed}} = 1 - \left(\frac{15 - \text{comp}}{\text{comp}^2}\right)^{1/3}$$

This formula only applies when comp < 15. When comp >= 15, the minimum possible CRAP score (at 100% coverage) is comp itself, which already meets or exceeds the threshold. In that case, coverage alone cannot bring the CRAP score below the threshold -- the method must be refactored to reduce its cyclomatic complexity first.

Report this as: "To bring ProcessOrder (complexity 12) below CRAP 15, increase coverage from 45% to at least 72%." For methods where complexity alone exceeds the threshold, report: "ComplexMethod (complexity 18) cannot reach CRAP < 15 through testing alone -- reduce complexity by extracting sub-methods."

Validation

  • Verify that coverage data was collected successfully (Cobertura XML exists and contains data)
  • Confirm every coverage figure came from that XML — no estimated, assumed, or source-comment-derived values
  • Cross-check that method names in coverage data match the source code
  • Confirm CRAP scores by spot-checking the formula on one method manually
  • Ensure a 100%-covered method's CRAP equals its complexity exactly

Common Pitfalls

  • Estimating coverage when collection fails: never do it — the resulting CRAP scores are wrong in the direction that matters. Work through the fallbacks in Step 1, then report the blocker instead.
  • Trusting a stale complexity comment in the source: compute cyclomatic complexity from the current code; a // complexity: 7 comment left by a previous author is not evidence.
  • Giving up on a shared-assembly or test-host collector error: dotnet-coverage collect runs out of process and usually succeeds where the in-proc collector fails.
  • Stale coverage data: Always regenerate coverage before computing CRAP scores. Old coverage files will produce misleading results.
  • Method name mismatches: Cobertura XML may use mangled/compiler-generated names for async methods, lambdas, or local functions. Match by line ranges when names don't align.
  • Generated code: Exclude auto-generated files (e.g., *.Designer.cs, *.g.cs) from analysis unless explicitly requested.

Version History

  • a3cb4a2 Current 2026-08-27 16:17
  • 7953ba8 2026-08-14 01:14

    支持经典.NET测试项目(非SDK风格),包括packages.config依赖和显式编译项的处理。

  • ad0cfe8 2026-08-01 21:34
  • ce75c35 2026-07-06 00:32

Same Skill Collection

.agents/skills/create-custom-agent/SKILL.md
.agents/skills/create-skill-test/SKILL.md
.agents/skills/create-skill/SKILL.md
.agents/skills/improve-skill-quality/SKILL.md
.github/skills/agentic-workflows/SKILL.md
eng/skill-validator/tests/fixtures/sample-skill/SKILL.md
plugins/dotnet-advanced/skills/csharp-scripts/SKILL.md
plugins/dotnet-advanced/skills/nuget-trusted-publishing/SKILL.md
plugins/dotnet-advanced/skills/vectorization/SKILL.md
plugins/dotnet-aspnetcore/skills/configuring-opentelemetry-dotnet/SKILL.md
plugins/dotnet-aspnetcore/skills/dotnet-webapi/SKILL.md
plugins/dotnet-aspnetcore/skills/minimal-api-file-upload/SKILL.md
plugins/dotnet-blazor/skills/create-blazor-project/SKILL.md
plugins/dotnet-blazor/skills/fetch-and-send-data/SKILL.md
plugins/dotnet-blazor/skills/support-prerendering/SKILL.md
plugins/dotnet-blazor/skills/use-js-interop/SKILL.md
plugins/dotnet-data/skills/optimizing-ef-core-queries/SKILL.md
plugins/dotnet-diag/skills/analyzing-dotnet-performance/SKILL.md
plugins/dotnet-diag/skills/clr-activation-debugging/SKILL.md
plugins/dotnet-diag/skills/dotnet-trace-collect/SKILL.md
plugins/dotnet-diag/skills/dump-collect/SKILL.md
plugins/dotnet-experimental/skills/exp-mock-usage-analysis/SKILL.md
plugins/dotnet-experimental/skills/exp-simd-vectorization/SKILL.md
plugins/dotnet-msbuild/skills/binlog-failure-analysis/SKILL.md
plugins/dotnet-msbuild/skills/copy-to-output-directory/SKILL.md
plugins/dotnet-msbuild/skills/msbuild-server/SKILL.md
plugins/dotnet-msbuild/skills/resolve-project-references/SKILL.md
plugins/dotnet-test-migration/skills/migrate-xunit-to-mstest/SKILL.md
plugins/dotnet-test-migration/skills/migrate-xunit-to-xunit-v3/SKILL.md
plugins/dotnet-test/skills/assertion-quality/SKILL.md
plugins/dotnet-test/skills/code-testing-extensions/SKILL.md
plugins/dotnet-test/skills/filter-syntax/SKILL.md
plugins/dotnet-test/skills/find-untested-sources/SKILL.md
plugins/dotnet-test/skills/test-tagging/SKILL.md
plugins/dotnet-upgrade/skills/dotnet-aot-compat/SKILL.md
.agents/skills/authoring-github-workflows/SKILL.md
plugins/dotnet-advanced/skills/dotnet-pinvoke/SKILL.md
plugins/dotnet-ai/skills/mcp-csharp-create/SKILL.md
plugins/dotnet-ai/skills/mcp-csharp-debug/SKILL.md
plugins/dotnet-ai/skills/mcp-csharp-test/SKILL.md
plugins/dotnet-ai/skills/technology-selection/SKILL.md
plugins/dotnet-aspnetcore/skills/convert-blazor-server-to-webapp/SKILL.md
plugins/dotnet-blazor/skills/author-component/SKILL.md
plugins/dotnet-blazor/skills/collect-user-input/SKILL.md
plugins/dotnet-blazor/skills/configure-auth/SKILL.md
plugins/dotnet-blazor/skills/coordinate-components/SKILL.md
plugins/dotnet-blazor/skills/plan-ui-change/SKILL.md
plugins/dotnet-data/skills/create-datadriven-aspnetcore/SKILL.md
plugins/dotnet-diag/skills/android-tombstone-symbolication/SKILL.md

Metadata

Files
0
Version
be7b560
Hash
f8620cb2
Indexed
2026-07-06 00:32

Accueil - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-28 06:39
浙ICP备14020137号-1 $Carte des visiteurs$