I'm currently writing unit tests for a Qt
project. I wanted to use the statistics provided in Jenkins through the Cobertura plugin (underneath gcov
is used to get the stats).
:~$ gcov -v
gcov 5.4.0 20160609
:~$ gcc -v
gcc version 5.4.0
However after I looked at the table (see below) I was really surprised to see the poor coverage especially of conditionals. For the first one (see Coverage Breakdown by File) I thought I was actually done, since the code has only three if
s(each with a single condition) and my tests covers all (checked this also through debugging just to make sure). So I am really confused what these numbers actually mean and how to interpret them in order to make my unit tests better.
I've even started thinking that some of the poor results might be due to the use of Qt
since it's not exactly pure C++ and all "extras" (slots, signals, MOC files etc.) might be something that gcov
can't handle properly.
Checking the annotated source listing with displayed red/green coverage markings should help.
The numbers inside the coloured bars are line counts, hence 47/108 means that 47 lines of code out of the 108 lines that are controlled by conditionals, have coverage.
For each conditional you need at least two unit tests: one for each branch.
If there are && or || in the conditions - or anywhere else (eg a logical expression) then each combination must be exercised to achieve 100%.
Also don't forget
a = (j == 0) ? c : d;
requires (at least) two tests !
Also, if using C++, see Why gcc 4.1 + gcov reports 100% branch coverage and newer (4.4, 4.6, 4.8) reports 50% for "p = new class;" line?