Search code examples
g++code-coveragegcovgcovr

Is it possible to merge coverage data from two executables with 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.

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

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 ?


Solution

  • 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;
    }
    

    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
    

    Which then for sut.c shows the following results:

    enter image description here

    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

    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.