C++项目Coveralls误报未测试文件覆盖率100%,如何修正为0%?
This issue occurs because cpp-coveralls only processes source files with corresponding .gcda or .gcno files by default. For files without test cases, if they weren’t compiled with coverage flags or aren’t explicitly included in the tool’s scope, Coveralls might incorrectly mark them as 100% covered instead of 0%. Here’s how to resolve it:
Ensure all source files use coverage compilation flags
Double-check that every source file in your project is built with-g -O0 -fprofile-arcs -ftest-coverage. Missing these flags means the file won’t generate a.gcnofile (which holds code structure information), andcpp-coverallswon’t recognize it as part of the coverage report.Explicitly include all source directories in
cpp-coveralls
Use the--includeparameter to force the tool to scan all your source files—even those without.gcdafiles (no test execution data). For example, if all your code lives in thesrc/directory:cpp-coveralls --include src/Add multiple
--includeflags for separate directories (e.g.,--include src/utils/ --include src/core/) if needed. This ensures untested files are included in the report and marked as 0% covered.Verify GCOV output first
Before runningcpp-coveralls, confirmgcovreports 0% coverage for an untested file directly. For example:gcov src/untested_file.cppIf
gcovshows the correct 0% coverage, the problem iscpp-coverallsnot including the file. If not, you missed adding coverage flags during compilation for that file.Avoid accidental exclusions
Check that--excludeflags aren’t skipping your project’s source files. Be specific with exclusions (e.g.,--exclude vendor/) to ensure only third-party code is omitted.Add
--gcov-optionsfor stricter parsing
Sometimes adding--gcov-options "-l"helps ensure accurate line-by-line coverage calculation for files with no execution data:cpp-coveralls --include src/ --gcov-options "-l"
After updating these settings, re-run your tests, generate the coverage report, and upload it to Coveralls. Untested files should now correctly show 0% coverage instead of 100%.
内容的提问来源于stack exchange,提问作者Ugnius Malūkas

