使用 lcov 对 .cpp 文件进行联合测试时排除头文件覆盖

Exclude the header files coverage while doing the unitests for .cpp files using lcov

我已经为单元测试生成了 html 代码覆盖率。问题是它为包含预处理器中定义的所有内容生成代码覆盖率。

单元测试的代码覆盖率在代码覆盖率报告的 unitTest link 中。我只需要 unitTest

在生成代码覆盖率之前,我还删除了所有其他内容并保留了测试文件的 gcda 和 gcno 文件,但它仍然无济于事。执行此操作的代码是:

find obj/ ! -name '*_unittests*' -type f -delete

代码覆盖率报告如下所示:

我尝试在包含预处理器的开头和结尾添加 LCOV_EXCL_START 和 LCOV_EXCL_STOP,如下所示,但仍然无济于事。

LCOV_EXCL_START
#include <inttypes.h> 
#include "../src/generators/buffersss.h" 
#include "gtest/gtest.h"
#include <getopt.h>  
LCOV_EXCL_STOP
Test code goes here.....

我做错了什么?

下面的代码解决了这个问题

lcov --remove coverage.info '/usr/include/*' 'src/*' -o filtered_coverage.info

对于 CMake 用户,我强烈建议使用:

https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmake

例如宏 setup_target_for_coverage_lcov 及其 EXCLUDE 参数。