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
- Install the C# Dev Kit extension
- Open Test Explorer (beaker icon)
- Click "Run Coverage Test"
- 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:
- Line Coverage — number of executed lines / total lines, the most basic metric
- Branch Coverage — number of executed branches / total branches, more accurate than line coverage
- 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:
- Phase 1: Cover core business logic (target 60-70%)
- Phase 2: Supplement boundary condition tests (target 70-80%)
- Phase 3: Handle exception scenarios (target 80-85%)
- 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
- Based on requirements analysis — List use cases, identify boundary conditions and exception situations
- Refer to complexity metrics — Cyclomatic complexity provides a lower bound for test cases
- Balance coverage and quality — Do not make 100% coverage the sole goal; focus on critical business logic
Four Major Test Types
- Boundary testing: Test the upper and lower limits of input values
- Exception testing: Verify error handling logic
- Main flow testing: Cover normal business processes
- 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%?
- Confirm that the
coverlet.collectorpackage is installed - Check whether the runsettings configuration is correct
- Confirm that the tests actually ran
- 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?
- Confirm that C# Dev Kit is installed
- Run "Run Coverage Tests" again
- Check whether the lcov file is generated
- Try reloading the window
Q4: How can coverage be improved?
- Identify uncovered critical code (red areas)
- Add boundary condition tests
- Test all conditional branches
- Add exception scenario tests
- Consider refactoring overly complex methods
Checklist
- Installed
coverlet.collectorpackage - 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 templatetemplates/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.runsettingsconfiguration file, configuring coverage collection parameters and exclusion rules - Modify the test project
.csproj, ensuring it includes thecoverlet.collectorpackage 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
- Day 06 - Code Coverage Practical Guide to Code Coverage
- Ironman contest article:https://ithelp.ithome.com.tw/articles/10374467
Official documentation
Tools
Related skills
unit-test-fundamentals- Unit testing fundamentals and FIRST principlesxunit-project-setup- xUnit project setup