MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

dotcover: dotcover print coverage results using cmd line

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.

StageCommandOutput
Collect coveragedotcover cover.snapshot file (binary, readable only by dotCover)
Machine-readable reportdotcover report --ReportType=JSONJSON with CoveragePercent, CoveredStatements, TotalStatements
Human-readable reportdotcover report --ReportType=HTMLBrowsable HTML report
Print or gatePowerShell ConvertFrom-Json or jqConsole output and the build's exit code

Prerequisites

  • The dotCover command-line tools. Install them as the JetBrains.dotCover.CommandLineTools NuGet package or as the dotCover .NET global tool. On Windows the executable is dotCover.exe. Either add it to PATH or call it by its full path.
  • A test runner to wrap, such as nunit3-console.exe, vstest.console.exe or dotnet test.
  • Debug symbols (.pdb files) 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 jq on 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

SymptomLikely causeFix
report says the snapshot file is not foundSnapshot name differs between cover --Output and report --SourceUse the same file name in both commands, or put it in a variable
Option lines fail as unknown commandsMulti-line command with no continuation characterEnd each line with a backtick (PowerShell), ^ (cmd) or \ (bash)
Unknown option or invalid argumentOption syntax differs in your dotCover versionCheck dotcover help cover and dotcover help report
Coverage is 0% or the report is emptyTests did not run, PDBs are missing, or filters exclude everythingCheck the test runner's output, deploy the .pdb files, review --Filters
$coverage.CoveragePercent prints nothingReport was generated as XML or HTML, or the wrong file was readConfirm --ReportType=JSON and the output path
Test assemblies counted in the totalNo filter on test projectsExclude them with a filter such as --Filters="-:module=*.Tests"

Best practices

  • Keep file names in variables ($snapshot = "coverage-results.snapshot") so the cover and report steps 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

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
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.

Related Posts

Jenkins Pipeline: Code coveragenusing JaCoCo

This guide shows how to measure Java code coverage with JaCoCo in a Jenkins declarative pipeline that builds with Gradle. It covers the stage above, the Gradle…

Read More

dotCover: Powershell Script code coverage report with dotCover

This page shows a PowerShell script that runs .NET unit tests under JetBrains dotCover, writes DetailedXML coverage files, and uses ReportGenerator to convert them to Cobertura and…

Read More

Code Coverage vs Test Coverage: Whatโ€™s Different and How to Use Both in CI

Code coverage and test coverage are often conflated, but they answer different quality questions. This guide explains the distinction, shows how they fit in CI, and provides practical measurement examples.

Read More

Code Coverage in DevOps: What It Measures, Why It Matters, and How to Use It in CI

Code coverage quantifies how much of your code is exercised by tests, helping teams find untested areas before release. This guide explains coverage types, CI workflow, practical commands, and how to avoid misusing the metric.

Read More

EMMA for Java Code Coverage: Setup, Instrumentation Modes, and J2EE Workflow

EMMA is a Java code coverage tool used to see which code paths are actually exercised by tests and runtime usage. This updated guide explains installation choices, instrumentation modes, and the offline J2EE workflow described in the original post.

Read More

Maven Clover for Distributed Java Applications: Instrument, Collect, and Aggregate Coverage

Learn a practical Clover + Maven workflow for distributed test environments. This walkthrough covers instrumentation, remote execution, coverage file collection, and final report generation.

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x