AgentSkillsCN

csharp-test-coverage

运行C#测试,同时进行代码覆盖率分析,并按类/方法行数与分支覆盖率生成报告,标明未覆盖的行号。支持可选的测试筛选器与可选的类筛选器。

SKILL.md
--- frontmatter
name: csharp-test-coverage
description: Run C# tests with code coverage and report per-class/method line and branch coverage with uncovered line numbers. Accepts an optional test filter and optional class filter.
allowed-tools: [Bash(dotnet test:*), Bash(reportgenerator:*), Bash(node:*), Bash(rm:*), Read]

Run C# tests with coverlet code coverage collection and produce a per-class coverage summary including uncovered line numbers for the classes you care about.

Arguments

  • $ARGUMENTS — An optional string with two parts (both optional):

    • Test filter: a dotnet test --filter expression (e.g. FullyQualifiedName~TreatmentBMPControllerTests)
    • Class filter: prefixed with --classes followed by comma-separated partial class names to include in the report (e.g. --classes TreatmentBMPs,TreatmentBMPController)

    If no test filter is provided, all tests are run. If no class filter is provided, all classes with >0% coverage are shown.

Examples

code
/csharp-test-coverage FullyQualifiedName~TreatmentBMPControllerTests --classes TreatmentBMPs,TreatmentBMPController
/csharp-test-coverage --classes Projects,ProjectController
/csharp-test-coverage FullyQualifiedName~ExtensionMethodsTest
/csharp-test-coverage

Steps

  1. Parse arguments from $ARGUMENTS:

    • Split on --classes to extract the test filter (before) and class name filters (after, comma-separated).
    • If no --classes flag, treat the entire argument as the test filter.
  2. Run tests with coverlet to collect coverage in cobertura format:

    code
    dotnet test Neptune.Tests/Neptune.Tests.csproj [--filter "<test-filter>"] -p:CollectCoverage=true -p:CoverletOutputFormat=cobertura -p:CoverletOutput=./coverage-results/ --no-restore
    
    • If a test filter was provided, include --filter "<test-filter>".
    • If tests fail, report the failure and stop.
  3. Extract per-class coverage with uncovered lines using the helper script:

    code
    node .claude/skills/csharp-test-coverage/parse-coverage.js Neptune.Tests/coverage-results/coverage.cobertura.xml [classFilter1,classFilter2,...]
    
    • If class name filters were provided, pass them as a comma-separated second argument.
    • The script outputs one JSON object per line with: class, file, lineCoverage, branchCoverage, uncoveredLines.
    • The uncoveredLines field contains line ranges (e.g. "18-20, 34-36") referring to the source file.
  4. Present results as a markdown table with columns: Class, File, Line%, Branch%, Uncovered Lines.

    • For any class with uncovered lines, use the Read tool to read those specific lines from the source file so you can describe what code paths are not covered.
    • Summarize what specific test scenarios are missing to cover those lines.
  5. Clean up temporary coverage files:

    code
    rm -rf Neptune.Tests/coverage-results/
    

Notes

  • The test project uses coverlet.msbuild (already a dependency in Neptune.Tests.csproj).
  • reportgenerator is installed as a global dotnet tool.
  • Coverage files are written to Neptune.Tests/coverage-results/ and cleaned up after reporting.
  • The --no-restore flag speeds up the test run by skipping NuGet restore.
  • The helper script parse-coverage.js lives alongside this SKILL.md and parses cobertura XML using Node.js.