Gcovr
Gcovr is a Python utility that processes code coverage data generated by the GNU gcov tool for C/C++ projects. It simplifies the creation of summarized code coverage reports in various formats, including text, HTML, XML (Cobertura, SonarQube, JaCoCo), JSON, CSV, and Markdown. Gcovr is currently at version 8.6 and maintains a regular release cadence with several updates throughout the year.
Warnings
- breaking Gcovr versions have dropped support for older Python versions. Version 7.0 dropped support for Python 3.7, and version 5.1 dropped Python 3.6 support. Version 5.0 removed support for Python 2 and Python 3.5.
- breaking Exit codes for errors in the reader or writer modules have changed. In gcovr 7.0, a reader error's exit code changed from 8 to 64, and a writer error's from 7 to 128.
- gotcha Forgetting to recompile your C/C++ code with coverage flags (`-fprofile-arcs -ftest-coverage` or `--coverage`) after making changes, or not cleaning old object files, can lead to outdated or incorrect coverage data. Also, using `-O` optimization flags can sometimes obscure coverage information.
- gotcha Gcovr relies on the `gcov` executable. If you have multiple GCC/Clang versions or custom installations, `gcovr` might pick the wrong `gcov` by default, leading to parsing errors or incorrect reports.
- gotcha On Windows, issues with path separators (backslashes vs. forward slashes) and symlinks can cause `gcovr` to fail to find source files or generate detailed reports, especially when source files are in different directories from the build output.
- breaking The internal data model for coverage information per line was improved in version 8.x. This removed the `function return count` from internal data model, HTML, and JSON output, and `function_name` for a line in JSON report is now always set.
Install
-
pip install gcovr
Imports
- gcovr
gcovr
Quickstart
# 1. Create a sample C++ file (example.cpp)
#include <iostream>
int foo(int param) {
if (param) {
return 1;
} else {
return 0;
}
}
int main() {
foo(0);
std::cout << "Hello from main!" << std::endl;
return 0;
}
# 2. Compile the code with coverage flags
g++ -fprofile-arcs -ftest-coverage -O0 example.cpp -o program
# 3. Run the compiled program to generate .gcda data files
./program
# 4. Generate a coverage report using gcovr (e.g., HTML report)
gcovr -r . --html --html-details -o coverage.html
# To view the report:
# open coverage.html