Integrating dotCover with CI/CD Pipelines (Azure DevOps, GitHub Actions)

dotCover: Complete Guide to .NET Code CoveragedotCover is a code coverage tool from JetBrains designed for .NET applications. It helps developers measure how much of their code is executed by automated tests, identify untested parts, and improve overall test quality. This guide covers dotCover’s key features, installation, usage patterns, integration with IDEs and CI/CD, interpreting reports, best practices, and troubleshooting.


What is Code Coverage and Why It Matters

Code coverage measures the fraction of code executed while running a test suite. Common metrics include:

  • Line coverage — percentage of source code lines executed.
  • Branch coverage — percentage of control-flow branches (if/else, switch) executed.
  • Method coverage — percentage of methods invoked during tests.

Why it matters:

  • Highlights untested code paths that could hide bugs.
  • Helps prioritize writing tests for critical or risky code.
  • Tracks coverage trends over time to prevent regressions.

While high coverage doesn’t guarantee correctness, it reduces blind spots and increases confidence in code changes.


Key Features of dotCover

  • Integrated with Visual Studio and Rider.
  • Supports multiple coverage metrics: line, branch, method.
  • Coverage highlighting in editor.
  • Coverage snapshots and historical comparisons.
  • Merging of coverage results from multiple test runs or machines.
  • Filtering (include/exclude) to focus on assemblies, namespaces, classes, or files.
  • Continuous Integration support (console runner, teamcity, Azure DevOps, GitHub Actions).
  • HTML and XML report generation.
  • Support for .NET Framework, .NET Core, .NET 5+.

Installation and Setup

  • Visual Studio: Install dotCover as part of ReSharper Ultimate or as a standalone extension where available. After installation, dotCover appears in the ReSharper menu.
  • Rider: dotCover functionality is built into JetBrains Rider; enable via settings if necessary.
  • Command-line: Download the dotCover console runner from JetBrains for CI usage.

Quick steps (Visual Studio):

  1. Install ReSharper Ultimate or dotCover extension.
  2. Restart Visual Studio.
  3. Open ReSharper → Unit Tests → Cover Unit Tests to run tests with coverage.

For CI:

  • Place dotCover console runner on the build agent or use JetBrains-provided tools.
  • Use the dotCover command-line parameters to run tests, collect coverage, and export reports.

Running Coverage Locally

In Visual Studio:

  • Use ReSharper → Unit Tests → Cover Unit Tests (or the dotCover menu) to run coverage for selected tests, projects, or the whole solution.
  • After the run, the Coverage Results window shows metrics and a tree view of assemblies/namespaces/classes.

In Rider:

  • Run tests with coverage from the Unit Tests window. Results appear with inline highlighting and a coverage tool window.

Coverage Snapshots:

  • Save snapshots to compare runs or keep historical evidence. Snapshots can be merged and exported.

Command-Line Usage (CI/CD)

dotCover console supports commands for starting coverage, running tests with specific runners (e.g., vstest.console, NUnit, xUnit), and generating reports.

Typical workflow:

  1. Collect coverage: use dotCover.exe cover with a configuration XML or command-line arguments specifying the test runner and target assemblies.
  2. Save snapshot: dotCover saves a .dcvr snapshot file.
  3. Export report: use dotCover.exe report to convert snapshots to HTML, XML, or JSON.

Example (conceptual):

dotCover.exe cover /TargetExecutable="vstest.console.exe" /TargetArguments:"MyTests.dll" /Output="coverage.dcvr" dotCover.exe report /Source="coverage.dcvr" /Output="coverage.html" /ReportType="HTML" 

Include filters to exclude third-party code, generated files, or test assemblies.


Integrating with CI Systems

  • Azure DevOps: Use dotCover console in build pipelines. Export HTML/XML reports and publish as pipeline artifacts. Some community tasks integrate dotCover directly.
  • GitHub Actions: Run dotCover in a job on Windows runners; upload coverage artifacts or publish via web pages.
  • TeamCity: JetBrains TeamCity has built-in support and can display dotCover statistics; dotCover can also be used with other CI servers by executing the console runner.

Tips:

  • Merge snapshots from parallel test jobs to get aggregated coverage.
  • Fail builds if coverage drops below thresholds using custom checks against exported XML.
  • Use filtering in CI to avoid counting generated or third-party code.

Interpreting Coverage Results

  • Focus on critical modules — not global percentage alone.
  • Use branch coverage to find untested conditional logic.
  • Identify hotspots: complex methods with low coverage.
  • Review excluded files (auto-generated, third-party) to ensure they’re intentionally excluded.

Coverage reports include:

  • A tree view by assembly → namespace → class → file.
  • Per-file source with green/red highlighting showing covered/uncovered lines.
  • Summary metrics and trends when comparing snapshots.

Best Practices

  • Aim for meaningful coverage: prioritize critical code paths and high-risk areas.
  • Keep tests fast and reliable; slow flaky tests reduce usefulness of coverage.
  • Use fine-grained filters to exclude non-business code (generated code, external libraries).
  • Enforce coverage gates in CI for important modules, not necessarily the whole solution.
  • Combine coverage metrics with mutation testing or static analysis for deeper quality checks.

Example strategy:

  • Require 80% coverage for core business assemblies, 60% for peripheral modules.
  • Run full coverage in nightly builds; run targeted coverage checks on pull requests.

Troubleshooting Common Issues

  • Missing coverage for async/await or generated code: enable symbol (.pdb) generation and ensure source maps are available.
  • Tests run but no coverage collected: check that the test runner is properly wrapped by dotCover and that filters are not excluding everything.
  • Performance overhead: collect coverage only when needed (PR or nightly) and use filtering to reduce instrumentation.
  • Merging snapshots shows inconsistent totals: ensure snapshots are from compatible builds and use consistent filters.

Alternatives and When to Use dotCover

dotCover is strong for developers using JetBrains tools and those wanting deep IDE integration. Alternatives include OpenCover, Coverlet, and Visual Studio’s built-in tools (Coverage in Enterprise). Choose dotCover when you want:

  • Tight ReSharper/Rider integration and in-editor highlighting.
  • Advanced snapshot and report features.
  • A supported, commercial tool with frequent updates.

Summary

dotCover provides a robust, developer-friendly way to measure and improve .NET code coverage. Use it to find untested code, enforce quality gates, and integrate coverage metrics into your development and CI workflows. Combine coverage results with targeted testing strategies and other quality tools to raise confidence in your codebase.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *