我喜欢在我的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;
}我可以编译它并在Linux上进行覆盖测试,例如
$ rm -f testprogram *.html *.gc??
$ gcc -o testprogram main.c \
-g --coverage -fprofile-arcs -ftest-coverage --coverage \
-DPRINTSTYLE=0
$ ./testprogram
$ gcovr -r . --html --html-details -o index.html
$ firefox index.main.c.html这几乎是超级的--但我想要做的是将-DPRINTSTYLE=0 (参见ahove)和-DPRINTSTYLE=1的测试结果结合起来--那么从逻辑上讲,我应该在生成的index.main.c.html中获得100%的覆盖率
我完全理解在中间需要重新编译。
如何使用带有ifdef代码的gcovr获得100%的覆盖率?
发布于 2019-11-07 17:37:43
这是可行的-但需要gcovr 4.2 (或更高版本),如https://gcovr.com/en/stable/guide.html#combining-tracefiles所示
首先安装或升级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.htmlhttps://stackoverflow.com/questions/58632496
复制相似问题