To print dotCover coverage results on the command line, run your tests with dotcover cover to create a snapshot, turn that snapshot into a JSON report with dotcover report, and then read the CoveragePercent value with PowerShell or jq. This page fixes the bugs in the example above and shows how to print per-assembly figures and fail a CI build when coverage drops below a threshold.
# first run the tests with coverage, generating a snapshot
dotcover cover
--targetExecutable="nunit3-console.exe"
--targetArguments="..."
--output="coverage-results.snapshot"
# then, generate the JSON report
dotcover report --Source="results.snapshot" --Output="coverage-results.json" --ReportType="JSON"
# then, generate the HTML report (a nice way of examining results manually)
dotcover report --Source="results.snapshot" --Output="coverage-results.html" --ReportType="HTML"
# Read the JSON file (my runners are on windows, so I'm using powershell)
$coverageJson = (Get-Content "coverage-results.json" -Raw) | ConvertFrom-Json
# Print the top level coverage result
Write-Host "Statements: $($coverageJson.CoveragePercent)%"Code language: PHP (php)
How the workflow fits together
dotCover's console runner works in two stages. The cover command starts a target process, such as a test runner, and records which statements run. It saves this data as a binary snapshot. The report command then converts the snapshot into a readable format: HTML for people, or JSON and XML for scripts. Printing a coverage number in a pipeline comes down to parsing the JSON report.
| Stage | Command | Output |
|---|---|---|
| Collect coverage | dotcover cover | .snapshot file (binary, readable only by dotCover) |
| Machine-readable report | dotcover report --ReportType=JSON | JSON with CoveragePercent, CoveredStatements, TotalStatements |
| Human-readable report | dotcover report --ReportType=HTML | Browsable HTML report |
| Print or gate | PowerShell ConvertFrom-Json or jq | Console output and the build's exit code |
Prerequisites
- The dotCover command-line tools. Install them as the
JetBrains.dotCover.CommandLineToolsNuGet package or as the dotCover .NET global tool. On Windows the executable isdotCover.exe. Either add it toPATHor call it by its full path. - A test runner to wrap, such as
nunit3-console.exe,vstest.console.exeordotnet test. - Debug symbols (
.pdbfiles) next to the assemblies under test. Without them dotCover cannot map coverage back to statements. - PowerShell 5.1 or later on Windows, or PowerShell 7 or
jqon Linux and macOS agents.
Note: JetBrains has changed the console runner's option names between releases. If an option shown here is rejected, run dotcover help cover and dotcover help report to see the exact syntax your installed version expects.
Corrected version of the example
The script above has three problems that stop it running as written. The cover step writes coverage-results.snapshot, but both report steps read results.snapshot, so they fail with a file-not-found error. The cover command is split across lines with no continuation character, so the shell runs dotcover cover with no arguments and then tries to run each --option line as a separate command. The --targetArguments="..." placeholder also has to be replaced with real test assemblies. Here is a working PowerShell version:
$ErrorActionPreference = 'Stop'
# 1. Run the tests under coverage and save a snapshot.
# PowerShell line continuation is a backtick (`) at the very end of the line.
dotcover cover `
--TargetExecutable="nunit3-console.exe" `
--TargetArguments="tests\MyApp.Tests.dll --noresult" `
--Output="coverage-results.snapshot"
# 2. JSON report for scripts (same snapshot name as above)
dotcover report --Source="coverage-results.snapshot" --Output="coverage-results.json" --ReportType="JSON"
# 3. HTML report for people
dotcover report --Source="coverage-results.snapshot" --Output="coverage-results.html" --ReportType="HTML"
# 4. Parse and print the overall result
$coverage = Get-Content "coverage-results.json" -Raw | ConvertFrom-Json
Write-Host ("Statements: {0}% ({1} of {2} covered)" -f `
$coverage.CoveragePercent, $coverage.CoveredStatements, $coverage.TotalStatements)Code language: PHP (php)
Expected output looks like Statements: 74% (1480 of 2000 covered). If you run the commands from cmd.exe rather than PowerShell, use ^ as the line-continuation character. In bash, use \.
Printing a per-assembly breakdown
The JSON report is a tree. The root holds the overall totals, and each node has Kind, Name, CoveragePercent, CoveredStatements, TotalStatements and a Children array that goes down through assemblies, namespaces, types and members. The first level of children is usually enough to find the weakest assemblies:
$coverage.Children |
Sort-Object CoveragePercent |
Format-Table Kind, Name, CoveragePercent, CoveredStatements, TotalStatements -AutoSizeCode language: JavaScript (javascript)
To list every type below a set percentage, walk the tree recursively:
function Get-CoverageNodes($node) {
foreach ($child in $node.Children) {
$child
Get-CoverageNodes $child
}
}
Get-CoverageNodes $coverage |
Where-Object { $_.Kind -eq 'Type' -and $_.CoveragePercent -lt 50 } |
Select-Object Name, CoveragePercentCode language: PHP (php)
Failing the build when coverage drops
Printing a number does not stop coverage from falling over time. A non-zero exit code makes any CI system (Azure Pipelines, GitHub Actions, TeamCity, Jenkins) mark the step as failed:
$threshold = 80
$actual = [double]$coverage.CoveragePercent
if ($actual -lt $threshold) {
Write-Host "Coverage $actual% is below the required $threshold%"
exit 1
}
Write-Host "Coverage $actual% meets the $threshold% threshold"Code language: PHP (php)
On Linux or macOS agents, jq does the same job in bash:
#!/usr/bin/env bash
set -euo pipefail
dotcover cover \
--TargetExecutable="dotnet" \
--TargetArguments="test tests/MyApp.Tests/MyApp.Tests.csproj" \
--Output="coverage-results.snapshot"
dotcover report --Source="coverage-results.snapshot" \
--Output="coverage-results.json" --ReportType="JSON"
pct=$(jq '.CoveragePercent' coverage-results.json)
echo "Statements: ${pct}%"
# Compare with awk because bash [ ] cannot compare decimal numbers
awk -v p="$pct" -v t=80 'BEGIN { exit (p < t) ? 1 : 0 }' \
|| { echo "Coverage below 80%"; exit 1; }Code language: PHP (php)
Note: Start with a threshold just below your current coverage and raise it over time. A target set too high on day one usually gets disabled rather than met.
Common errors and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
report says the snapshot file is not found | Snapshot name differs between cover --Output and report --Source | Use the same file name in both commands, or put it in a variable |
| Option lines fail as unknown commands | Multi-line command with no continuation character | End each line with a backtick (PowerShell), ^ (cmd) or \ (bash) |
Unknown option or invalid argument | Option syntax differs in your dotCover version | Check dotcover help cover and dotcover help report |
| Coverage is 0% or the report is empty | Tests did not run, PDBs are missing, or filters exclude everything | Check the test runner's output, deploy the .pdb files, review --Filters |
$coverage.CoveragePercent prints nothing | Report was generated as XML or HTML, or the wrong file was read | Confirm --ReportType=JSON and the output path |
| Test assemblies counted in the total | No filter on test projects | Exclude them with a filter such as --Filters="-:module=*.Tests" |
Best practices
- Keep file names in variables (
$snapshot = "coverage-results.snapshot") so thecoverandreportsteps cannot drift apart again. - Exclude test projects and generated code with filters so the percentage reflects production code.
- Publish the HTML report as a build artifact. The console number shows *whether* coverage changed, and the HTML report shows *where*.
- Do not commit snapshot files to source control. They are large binaries tied to a specific build.
- Treat coverage as a signal, not a goal. High coverage from tests with no assertions proves very little.
For more PowerShell techniques used in this script, such as pipelines, Where-Object and formatting output, see the PowerShell command line reference.
Frequently asked questions
Can dotCover print the coverage percentage directly without parsing a file?
The console runner writes coverage data to files rather than printing a summary metric, so the usual approach is to export a JSON or XML report and parse it. Build servers like TeamCity have built-in dotCover integration that shows the figures without a script.
What is the difference between a dotCover snapshot and a report?
A snapshot is dotCover's binary format. Only dotCover can open it, and you can merge several snapshots into one. A report (HTML, JSON or XML) is generated from a snapshot and can be read by people or scripts.
Does dotCover command line work on Linux and macOS?
Yes. For .NET (Core and later) applications the dotCover command-line tools run on Linux and macOS as well as Windows. The jq-based bash script on this page works on those agents.
How do I exclude test projects from dotCover coverage?
Pass a filter to the cover command, for example --Filters="-:module=*.Tests", which excludes any assembly whose name ends in .Tests. Check dotcover help cover for the filter syntax your version supports.
Why does my dotCover report show 0% coverage?
The most common causes are tests that never ran, missing .pdb files for the assemblies under test, and filters that exclude every module. Check the test runner's output inside the dotCover log first.
Related tutorials
- Powershell Tutorials: Command Line reference
- More Shell Scripting tutorials
- More Web Development tutorials
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals