Search code examples
c++code-coveragegcovr

gcovr folder level summaries


I have a project structured like this:

/path/to/source/A/ChildA1/A1.cpp
/path/to/source/A/ChildA2/A2.cpp
/path/to/source/B/ChildB1/B1.cpp
/path/to/source/B/ChildB2/B2.cpp

The highest level coverage report shows me summaries for each folder that directly contains code:

/path/to/source/A/ChildA1/
/path/to/source/A/ChildA2/
/path/to/source/B/ChildB1/
/path/to/source/B/ChildB2/

Is there any way I can also get a report at levels that do not directly include code? That is, just for:

/path/to/source/A/
/path/to/source/B/

Solution

  • I have been working with gcovr since few months now. I was creating reports for source level only. But yeah it can be done for your purpose too.

    One way of doing it is by extracting ( grep or findstr ) data of that particular folder from that whole report.

    For example the report we get from gcovr is like this:

    ------------------------------------------------------------------------------
                           GCC Code Coverage Report
    Directory: ...../src/
    ------------------------------------------------------------------------------
    File                                       Lines    Exec  Cover   Missing
    ------------------------------------------------------------------------------
    src/A/A1/xyz.cpp                            1609       2     0%   97,99,101....
    src/A/A2/abcg.cpp                            271       4     1%   .......
    src/B/B1/mnop.cpp                             74       2     2%   34,42,56-.....
    src/B/B2/wrds.cpp                           1533       6     0%   76,83,85-.....
    src/C/C1/abcdefg.cpp                        1079       8     0%   143,150,152.....
    

    So you can simply grep statements by specifying directory structure like "src/A" and from all the lines that are grep-ed take the second field sum and third field sum to get the coverage of that directory.

    It seems hectic but yeah it takes a simple 3-4 line script to do it.

    Script would be something like:

    python gcovr -d -r src/directory/ > ABCcoverage.txt
    while read -r DIRPATH ; do
        grep "$DIRPATH" ABCcoverage.txt > temp.txt
        #ln is lines in file and ex is no. of executed lines
        lnsum=0 ; exsum=0
        while read -r LINE ; do
            ln=$(echo $LINE | awk '{print $2}')
            ex=$(echo $LINE | awk '{print $3}')
            lnsum=$(expr $lnsum + $ln)
            exsum=$(expr $exsum + $ex)
        done < "temp.txt"
        echo $DIRPATH, lines:$lnsum, executed:$exsum, covered %:$(expr ( $exsum / $lnsum ) \* 100 ) >> IntendedResult.txt
    done < "DIRLIST.txt"
    

    There are much much more convenient ways of performing arithmetics though. So feel free to make edits people. :D

    Another way of doing it is by generating an xml report instead of this console report. It uses cobertura xml format and so has package tags (package for each directory structure). But trust me it would be very tiresome to write a code to count each line and check if it has any hits for each package.