Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何使用带有ifdef代码的gcovr获得100%的覆盖率?_Linux_Code Coverage_Gcov_Test Coverage_Gcovr - Fatal编程技术网

Linux 如何使用带有ifdef代码的gcovr获得100%的覆盖率?

Linux 如何使用带有ifdef代码的gcovr获得100%的覆盖率?,linux,code-coverage,gcov,test-coverage,gcovr,Linux,Code Coverage,Gcov,Test Coverage,Gcovr,我喜欢在我的Linux机器上使用gcovr,以了解哪些经过测试,哪些没有。 我掉进了一个看不到解决办法的坑里 我有如下所示的C代码(另存为main.C)。代码变得非常简单——实际上,重点只是#if构造以及如何使用不同编译设置的覆盖率分析 /* Save as main.c */ #include <stdio.h> void fct(int a) { // Define PRINTSTYLE to 0 or 1 when compiling #if PRINTSTYLE==0

我喜欢在我的Linux机器上使用gcovr,以了解哪些经过测试,哪些没有。 我掉进了一个看不到解决办法的坑里

我有如下所示的C代码(另存为
main.C
)。代码变得非常简单——实际上,重点只是#if构造以及如何使用不同编译设置的覆盖率分析

/* Save as main.c */
#include <stdio.h>

void fct(int a)
{
// Define PRINTSTYLE to 0 or 1 when compiling
#if PRINTSTYLE==0
    if (a<0) {
        printf("%i is negative\n", a);
    } else {
        printf("%i is ... sorta not negative\n", a);
    }
#else
    if (a<0) {
        printf("%i<0\n", a);
    } else {
        printf("%i>=0\n", a);
    }
#endif
}


int main(void)
{
    fct(1);
    fct(-1);
    return 0;
}
这几乎是超级的-但我想做的是将
-DPRINTSTYLE=0
(请参见ahove)和
-DPRINTSTYLE=1
)的测试结果结合起来-然后我逻辑上应该在生成的index.main.c.html中获得100%的覆盖率

<>我完全理解中间需要重新编译。


如何使用带有ifdef代码的gcovr获得100%的覆盖率?

这是可行的,但需要gcovr 4.2(或更高版本),如中所示

首先安装或升级gcovr,例如使用

pip install -U gcovr
然后确保~/.local/bin/位于$PATH中

接下来,为每个配置运行一次gcovr,并生成JSON报告:

gcc -o testprogram main.c -g --coverage -DPRINTSTYLE=0
./testprogram
gcovr -r . --json run-1.json

gcc -o testprogram main.c -g --coverage -DPRINTSTYLE=1
./testprogram
gcovr -r . --json run-2.json
最后,使用-a/--add tracefile模式组合JSON报告并生成所需的报告:

gcovr --add-tracefile run-1.json --add-tracefile run-2.json --html-details coverage.html
参考:

gcovr --add-tracefile run-1.json --add-tracefile run-2.json --html-details coverage.html