This document explains how to generate and view code coverage reports for the XHarness project.
- .NET 6+ SDK
- (Optional) ReportGenerator for HTML reports:
dotnet tool install -g dotnet-reportgenerator-globaltool
./run-coverage.sh [Configuration] [Format]Examples:
# Run with default settings (Debug, cobertura)
./run-coverage.sh
# Run Release configuration with OpenCover format
./run-coverage.sh Release opencover
# Run Debug with JSON format
./run-coverage.sh Debug json.\run-coverage.ps1 -configuration [Configuration] -format [Format]Examples:
# Run with default settings (Debug, cobertura)
.\run-coverage.ps1
# Run Release configuration with OpenCover format
.\run-coverage.ps1 -configuration Release -format opencoverCoverlet supports multiple output formats:
- cobertura - Cobertura XML format (default, best for CI/CD)
- opencover - OpenCover XML format
- json - JSON format
- lcov - LCOV format
After running tests with coverage:
# Install ReportGenerator (one-time)
dotnet tool install -g dotnet-reportgenerator-globaltool
# Generate HTML report from Cobertura format
reportgenerator \
-reports:"artifacts/coverage/**/coverage.cobertura.xml" \
-targetdir:"artifacts/coverage/report" \
-reporttypes:Html
# Open the report
open artifacts/coverage/report/index.html # macOS
xdg-open artifacts/coverage/report/index.html # Linux
start artifacts/coverage/report/index.html # WindowsInstall one of these VS Code extensions:
These extensions will show coverage directly in the editor.
# Install reportgenerator if not already installed
dotnet tool install -g dotnet-reportgenerator-globaltool
# Generate summary to console
reportgenerator \
-reports:"artifacts/coverage/**/coverage.cobertura.xml" \
-reporttypes:TextSummaryIf you prefer more control, you can run individual test projects:
# Run a specific test project with coverage
dotnet test tests/Microsoft.DotNet.XHarness.CLI.Tests/Microsoft.DotNet.XHarness.CLI.Tests.csproj \
--collect:"XPlat Code Coverage" \
--results-directory artifacts/coverage \
--settings tests/coverlet.runsettingsCoverage behavior is configured in tests/coverlet.runsettings:
- Exclude: Assemblies/types to exclude from coverage (e.g., test assemblies, third-party libraries)
- ExcludeByAttribute: Exclude members with specific attributes (e.g.,
[GeneratedCode]) - ExcludeByFile: Exclude specific files or patterns
- SkipAutoProps: Skip auto-implemented properties
- UseSourceLink: Use SourceLink for better source file mapping
Edit tests/coverlet.runsettings to:
- Add more exclusions
- Change output formats
- Adjust threshold values
- Include/exclude specific assemblies
Example exclusions:
<Exclude>[*.Tests]*,[xunit.*]*,[Moq]*,[System.*]*</Exclude>Add this to your pipeline YAML:
- task: DotNetCoreCLI@2
displayName: 'Run Tests with Coverage'
inputs:
command: 'test'
projects: '**/*Tests.csproj'
arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage" --settings:tests/coverlet.runsettings'
- task: PublishCodeCoverageResults@1
displayName: 'Publish Coverage Results'
inputs:
codeCoverageTool: 'Cobertura'
summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'Add this to your workflow:
- name: Run Tests with Coverage
run: ./run-coverage.sh Release cobertura
- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v3
with:
files: artifacts/coverage/**/coverage.cobertura.xml
fail_ci_if_error: trueYou can enforce minimum coverage thresholds by adding to test project .csproj files:
<PropertyGroup>
<CoverletOutputFormat>cobertura</CoverletOutputFormat>
<Threshold>80</Threshold>
<ThresholdType>line,branch,method</ThresholdType>
<ThresholdStat>total</ThresholdStat>
</PropertyGroup>This will fail the build if coverage falls below the threshold.
- Ensure test assemblies are being excluded:
<Exclude>[*.Tests]*</Exclude> - Check that source projects are referenced, not just DLLs
- Verify the
IncludeTestAssemblysetting isfalse
- Enable SourceLink:
<UseSourceLink>true</UseSourceLink> - Ensure PDB files are being generated
- Check that source file paths are correct
- Use
<SingleHit>true</SingleHit>to record only first hit per line - Exclude more assemblies/files
- Run specific test projects instead of entire solution