C++ Code Coverage with CMake and GCOVR
Bensuperpc August 07, 2025 [Software] #Features #Code coverage #Testing #GCOVRCAN bus under Linux (WIP)
Benchmarking Webp format (WIP)
Tutorial on FFMPEG (WIP)
A guide to SFF PCs (WIP)
RTL9210 Firmware and Tools
Comparison of SSDs
Evolutions of CPUs (WIP)
Mesa GPU drivers
Deploy Traefik as a reverse proxy with Kubernetes
C++ Code Coverage with CMake and GCOVR
Code Coverage in C/C++ with CMake and GCOVR
Introduction
This tutorial shows how to set up a C/C++ project with CMake and GCOVR to generate code coverage reports. This allows you to precisely verify which parts of the source code and which conditional branches (if, switch, etc.) are actually covered by the tests.
We’ll use a small sample project based on Google Test, but the method is also compatible with other frameworks such as Catch2, Boost.Test, or Doctest.
Minimum Requirements
- CMake 3.15
- GCC or Clang (C++17)
- Google Test (GTest)
- GCOVR 5.0
- Ninja (optional, but recommended)
A distribution like Ubuntu 22.04, Debian 12 or higher is recommended for this tutorial, but you can use any Linux distribution with the required versions of the tools.
Installing Dependencies
To install the necessary dependencies, you can use the following commands:
For Ubuntu/Debian :
For Fedora :
For Arch Linux :
What is GCOVR?
GCOVR is an open-source tool that generates C/C++ code coverage reports from the data produced during test execution.
Sample Project
The sample project is a simple application and two C++ libraries, one for mathematics and one for physics.
Here is the project structure:
You can download the sample project: tuto_coverage.7z
Compilation with Coverage
To enable code coverage, the project must be compiled with the options: -O0 -g --coverage
and --coverage
for linking.
&&
The --coverage
option is equivalent to -fprofile-arcs -ftest-coverage
for GCC/Clang, in a classic project, it is preferable to use CMake presets rather than specifying the options manually.
Running Tests
Once the project is compiled, you can run the tests, this will generate files on which GCOVR relies to generate coverage reports:
.gcno
generated at compile time, contains metadata for coverage.gcda
generated at runtime, contains the collected coverage data
Option | Description |
---|---|
--test-dir build | Specify the directory containing the tests |
--output-on-failure | Show the output of failed tests |
--no-tests=error | Generates an error if no tests are found |
--repeat until-fail:1 | Repeats the tests until a failure occurs, here 1 time |
--schedule-random | Runs the tests in random order |
--parallel 1 | Runs the tests in parallel, here 1 test at a time |
You will get output similar to:
=
)
HTML Report with GCOVR
After running the tests, you can generate a coverage report with GCOVR, here we generate an HTML report to visualize the code coverage, but you can also generate reports in plain text, XML or JSON.
Option | Description |
---|---|
--root "." | Sets the root directory for coverage |
--decisions | Includes coverage decisions in the report |
--calls | Includes function calls in the report |
--html-theme "green" | Sets the HTML report theme to green |
--exclude "tests/*" | Excludes test files from the report |
--exclude "build/*" | Excludes build files from the report |
--exclude "main.cpp" | Excludes the main.cpp file from the report |
--html | Generates an HTML report |
--html-details | Includes details in the HTML report |
--output "build/coverage.html" | Specifies the output file name for the HTML report |
Once the HTML report is generated, you can open it in your browser to visualize the code coverage, it should be generated in the file build/coverage.html
.
Here is the rendering of the report in the browser:


XML or JSON Report
You can also generate a report in XML or JSON for integration with other tools:
Ou en JSON :