是否可以使用gcov/gcovr合并来自两个可执行文件的coverage数据? [英] Is it possible to merge coverage data from two executables with gcov/gcovr?

查看:422
本文介绍了是否可以使用gcov/gcovr合并来自两个可执行文件的coverage数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个项目中,我正在用不同的选项编译的三个不同的可执行文件上运行测试用例.根据选项的不同,是否采用某些代码路径.现在,我只使用一个可执行文件中的coverage数据.

On one project, I'm running the test cases on three different executables, compiled with different options. Depending on the options, some code paths are taken or not. Right now, I'm only using the coverage data from one executable.

我正在使用gcovr生成XML,然后由Sonar对其进行解析:

I'm using gcovr to generate a XML that is then parsed by Sonar:

gcovr -x -b -r . --object-directory=debug/test > coverage_report.xml

我有三套gcda和gcno文件,但是我不知道如何生成它们的全局报告.

I have three sets of gcda and gcno files, but I don't know how to generate a global report of them.

有没有办法做到这一点?

Is there any way to do that ?

推荐答案

假定通过使用不同的选项进行编译"表示您进行编译,以便在lcov的帮助下在预处理后获得不同的输出(如k0n3ru所述) )我能够做到.这是文件sut.c中的示例代码:

Assuming that by "compiled with different options" you mean that you compile such that you obtain different outputs after preprocessing, with the help of lcov (as mentioned by k0n3ru) I was able to do it. Here's the sample code in file sut.c:

#include "sut.h"
#include <limits.h>

int foo(int a) {
#if defined(ADD)
    a += 42;
#endif
#if defined(SUB)
    a -= 42;
#endif
    return a;
}

使用sut.h仅提供foo的声明,并在test.c中提供一个简单的main,该main调用foo并输出结果.然后,使用此命令序列,我可以创建一个sut.c覆盖率100%的total.info文件:

with sut.h only providing the declaration of foo, and a simple main in test.c, which calls foo and prints the results. Then, with this sequence of commands I was able to create a total.info file with 100% coverage for sut.c:

> g++ --coverage -DADD test.c sut.c -o add.out
> ./add.out
> lcov -c -d . -o add.info   # save data from .gdda/.gcno into add.info
> g++ --coverage -DSUB test.c sut.c -o sub.out
> ./sub.out
> lcov -c -d . -o sub.info   # save again, this time into sub.info
> lcov -a add.info -a sub.info -o total.info  # combine them into total.info
> genhtml total.info

然后sut.c显示以下结果:

Which then for sut.c shows the following results:

编辑(感谢Gluttton提醒我添加这一部分):然后,应借助此处提供的"lcov至cobertura XML转换器",可以将lcov格式的total.info文件转到Cobertura XML输出. (尽管我还没有尝试过): https://github.com/eriwen/lcov- to-cobertura-xml

EDIT (Thanks to Gluttton for reminding me of adding this part): Going from the total.info file in lcov format to the Cobertura XML output should then be possible with the help of the "lcov to cobertura XML converter" provided here (although I have not tried that): https://github.com/eriwen/lcov-to-cobertura-xml

但是可以合并承保范围信息这一事实当然并不意味着这样做是一个好主意:IMO承保范围在测试套件质量方面的信息价值有限.合并来自不同预处理器输出的覆盖结果将进一步降低该值.

The fact that merging of coverage information is possible, however, does certainly not mean that it is a good idea to do so: Coverage, IMO, has only limited informative value regarding the quality of a test suite. Merging coverage results from different preprocessor outputs will even further decrease this value.

这是因为将减少开发人员了解他们未考虑的场景的可能性:通过使用条件编译,代码的控制结构和数据流在预处理器输出之间可能会发生巨大变化-由覆盖"产生的覆盖信息来自不同预处理器输出的测试运行结果可能无法对结果进行有意义的解释.

This is because the possibilities for developers to learn about scenarios they have not considered will be reduced: By using conditional compilation the control structure and data flow of the code can vary tremendously between preprocessor outputs - coverage information that results from 'overlaying' results from test runs for different preprocessor outputs can make a meaningful interpretation of the results impossible.

这篇关于是否可以使用gcov/gcovr合并来自两个可执行文件的coverage数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆