Code Coverage in NTools
The NTools build system includes comprehensive code coverage support that can be easily configured and integrated into your build pipeline.
Overview
Code coverage is automatically collected when running the TEST target if the EnableCodeCoverage property is set to true (which is the default). The system uses XPlat Code Coverage collection and ReportGenerator for creating detailed HTML and text reports.
Configuration
Basic Configuration
The following properties control code coverage behavior:
| Property | Default Value | Description |
|---|---|---|
EnableCodeCoverage |
true |
Controls whether code coverage is collected during test runs |
CoverageAssemblyFilters |
+*;-*Tests*;-*Test* |
Filters for assemblies to include/exclude from coverage |
CoverageClassFilters |
+*;-*.Tests.*;-*.Test.* |
Filters for classes to include/exclude from coverage |
Customizing Coverage Filters
You can customize coverage filters by setting properties in your project file:
<PropertyGroup>
<EnableCodeCoverage>true</EnableCodeCoverage>
<CoverageAssemblyFilters>+MyProject*;-*Tests*;-*Mock*</CoverageAssemblyFilters>
<CoverageClassFilters>+*;-*.Tests.*;-*.Mocks.*</CoverageClassFilters>
</PropertyGroup>
Filter Syntax
Coverage filters use the following syntax:
- + includes the pattern
- - excludes the pattern
- * is a wildcard
- Filters are separated by semicolons (;)
MSBuild Targets
COVERAGE Target
The COVERAGE target generates comprehensive code coverage reports:
nb COVERAGE
This target:
1. Installs ReportGenerator tool if not present
2. Processes coverage files from test results
3. Generates HTML reports in CoverageReport folder
4. Creates text summaries for CI/CD integration
5. Copies coverage files for GitHub Actions
COVERAGE_SUMMARY Target
For a quick coverage overview:
nb COVERAGE_SUMMARY
This displays high-level coverage metrics without generating full reports.
TEST Target with Coverage
The TEST target automatically includes coverage collection when EnableCodeCoverage is true:
nb TEST
This runs all tests and collects coverage data in a single step.
Output Files
After running coverage targets, you'll find:
HTML Reports
CoverageReport/index.html- Main coverage reportCoverageReport/- Detailed HTML coverage reports
Text Reports
- Console output with coverage summary
- Coverage files copied to artifacts for CI/CD
Coverage Data
- Raw coverage files in test results directory
- Processed coverage data for further analysis
CI/CD Integration
GitHub Actions
Coverage reports are automatically copied to appropriate locations for GitHub Actions integration. The COVERAGE target handles this automatically.
Disabling Coverage
To disable coverage collection (e.g., for faster builds):
<PropertyGroup>
<EnableCodeCoverage>false</EnableCodeCoverage>
</PropertyGroup>
Or via command line:
nb TEST -p:EnableCodeCoverage=false
Examples
Basic Usage
# Run tests with coverage
nb TEST
# Generate coverage reports
nb COVERAGE
# Quick coverage summary
nb COVERAGE_SUMMARY
Custom Configuration
<PropertyGroup>
<EnableCodeCoverage>true</EnableCodeCoverage>
<CoverageAssemblyFilters>+MyProject*;+MyLibrary*;-*Tests*</CoverageAssemblyFilters>
<CoverageClassFilters>+*;-*.Tests.*;-*TestHelpers*</CoverageClassFilters>
</PropertyGroup>
Full Build Pipeline
# Complete build with tests and coverage
nb STAGE