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

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

问题描述

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

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,它调用 foo 并打印结果.然后,通过这一系列命令,我能够创建一个 100% 覆盖 sut.c 的 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 to 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 合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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