SuperAPI AI Directory

dotnet-testing-code-coverage-analysis

Agents & models

View repository

Updated 10-15 23:45

About this skill

Code Coverage Analysis Guide

Core Concepts of Code Coverage

Code Coverage is a measurement metric used to count how much code is actually executed when tests run.

Practical value: Identify testing blind spots, assess test completeness, assist refactoring decisions, increase testing confidence.

Common misconceptions:

  • 100% coverage does not mean there are no bugs — it only means the code was executed, not that correct behavior was verified
  • A higher coverage number is not necessarily better — what matters is the effectiveness of the tests
  • Treating coverage as a KPI will backfire — developers will write tests without Assert just to inflate the numbers

Coverage Tool Selection

Tool Advantages Applicable scenarios
Visual Studio Enterprise Built-in integration, complete UI Enterprise only
Fine Code Coverage (recommended) Free, real-time display, directly marked in the editor All VS versions
.NET CLI + Coverlet Cross-platform, CLI automation CI/CD pipelines
VS Code built-in Cross-platform, no additional extensions needed VS Code development

Fine Code Coverage Setup

  • Installation method: Visual Studio Extension Manager → search "Fine Code Coverage" → install
  • Required settings: Tools → Options → Fine Code Coverage → Enable: True、Editor Colouring Line Highlighting: True

VS Code Built-in Test Coverage

  1. Install the C# Dev Kit extension
  2. Open Test Explorer (beaker icon)
  3. Click "Run Coverage Test"
  4. View results: Test Coverage view, in-editor display, File Explorer display

Running Coverage Analysis

# .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:開啟測試總管 → 點選「執行涵蓋範圍測試」

Method Description

Method Tool Steps
.NET CLI Coverlet + CLI Run dotnet test --collect:"XPlat Code Coverage"
Fine Code Coverage VS extension Automatically displayed after running tests in VS
VS Code C# Dev Kit Open Test Explorer → Run Coverage Test

Configuring Coverlet

The test project needs to install the coverlet.collector package, and advanced configuration (exclusion rules, thresholds, report formats) can be done through the runsettings file.

For a complete csproj configuration example, refer to references/coverlet-csproj-config.md

Interpreting Coverage Reports

Color coding: Green (covered), Yellow (partially covered), Red (not covered)

Coverage metrics:

  1. Line Coverage — number of executed lines / total lines, the most basic metric
  2. Branch Coverage — number of executed branches / total branches, more accurate than line coverage
  3. Method Coverage — number of executed methods / total methods

Report Interpretation Strategy: Prioritize red areas (completely untested) → check yellow areas (some branches untested) → assess necessity (simple getters/setters can be skipped)

Combining with Complexity Metrics

Cyclomatic Complexity represents the number of independent logical paths in a program, equal to the minimum number of test cases required. Each if, for, while, case, &&, || increases complexity.

Visual Studio Extensions:

  • CodeMaintainability — Displays maintainability metrics, calculates cyclomatic complexity
  • CodeMaid — Spade visualizes code structure, displays the complexity of each method

For complete examples and testing strategies, please refer to references/cyclomatic-complexity-example.md

Strategies for Improving Coverage

Incremental Improvement Process:

  1. Phase 1: Cover core business logic (target 60-70%)
  2. Phase 2: Supplement boundary condition tests (target 70-80%)
  3. Phase 3: Handle exception scenarios (target 80-85%)
  4. Maintenance phase: New features must have tests

Priority:

  • High priority: Core business logic, financial calculations, data validation, access control, exception handling
  • Medium priority: Data transformation, formatting logic, query logic
  • Low priority: Simple getters/setters, DTO classes, auto-generated code

Exclude unnecessary code: Use the [ExcludeFromCodeCoverage] attribute or exclude in runsettings.

Best Practices

Deciding the Number of Test Cases

  1. Based on requirements analysis — List use cases, identify boundary conditions and exception situations
  2. Refer to complexity metrics — Cyclomatic complexity provides a lower bound for test cases
  3. Balance coverage and quality — Do not make 100% coverage the sole goal; focus on critical business logic

Four Major Test Types

  1. Boundary testing: Test the upper and lower limits of input values
  2. Exception testing: Verify error handling logic
  3. Main flow testing: Cover normal business processes
  4. Conditional branch testing: Ensure all branches have tests

Continuous Improvement

  • Check coverage changes before each commit, review coverage during Pull Requests
  • Focus on uncovered critical code, prioritize high-complexity untested areas
  • New features must include tests, Code Review includes test checks

CI/CD Integration

Coverage analysis can be integrated into CI/CD Pipelines, using dotnet test --collect:"XPlat Code Coverage" with reportgenerator in GitHub Actions to generate reports; using the DotNetCoreCLI@2 task with PublishCodeCoverageResults@1。

For complete YAML configuration examples, please refer to references/cicd-integration.md

Common Issues and Solutions

Q1: Coverage shows 0%?

  1. Confirm that the coverlet.collector package is installed
  2. Check whether the runsettings configuration is correct
  3. Confirm that the tests actually ran
  4. Check whether exclusion settings are too broad

Q2: Cannot see coverage in Visual Studio?

  • Community/Professional editions: Install the Fine Code Coverage extension
  • Enterprise edition: Use built-in features
  • Confirm that coverage collection is enabled

Q3: VS Code cannot display coverage?

  1. Confirm that C# Dev Kit is installed
  2. Run "Run Coverage Tests" again
  3. Check whether the lcov file is generated
  4. Try reloading the window

Q4: How can coverage be improved?

  1. Identify uncovered critical code (red areas)
  2. Add boundary condition tests
  3. Test all conditional branches
  4. Add exception scenario tests
  5. Consider refactoring overly complex methods

Checklist

  • Installed coverlet.collector package
  • Can run dotnet test --collect:"XPlat Code Coverage"
  • The tool can properly display coverage results
  • Understand the true meaning of coverage numbers (not a KPI)
  • Unnecessary code has been excluded
  • Focus on test quality rather than coverage numbers

Template files

  • templates/runsettings-template.xml - Coverage configuration template
  • templates/coverage-workflow.md - Complete workflow instructions

Core philosophy

Code coverage is a means, not an end. The focus is on whether all critical business logic is tested, whether the tests truly verify the expected behavior, and whether they can provide confidence during refactoring.

Output format

  • Generate coverage.runsettings configuration file, configuring coverage collection parameters and exclusion rules
  • Modify the test project .csproj, ensuring it includes the coverlet.collector package reference
  • Generate a coverage report (cobertura XML format), usable by CI/CD pipelines
  • Provide a coverage analysis summary, indicating uncovered critical areas and improvement suggestions

Reference resources

Original article

Official documentation

Tools

Related skills

  • unit-test-fundamentals - Unit testing fundamentals and FIRST principles
  • xunit-project-setup - xUnit project setup