0

I've been trying to get which condition on a branch my executed test case took. For example, this is a snip of the coverage information I got from Gcov's gcov -b (I also use -i option for readability):

lcount:10,1
branch:10,nottaken 
branch:10,taken    

After examining some samples, it seems that the true condition always written first on every branch information. Which means I can determine whether the executed test case take the true part or false part of the branch. And in this case, the test case took the false part of the branch on line 10.

Now, here is a snip from a generated xml by Gcovr's --branches and --xml of the same program and test case:

<line branch="true" condition-coverage="50% (1/2)" hits="1" number="10">
   <conditions>
      <condition coverage="50%" number="0" type="jump"/>
   </conditions>
</line>

Here, I can't figure out which part of the branch was taken.

Is there any options on Gcovr that I can use?

AceVez
  • 291
  • 1
  • 5
  • 19

1 Answers1

2

The gcovr XML output uses the Cobertura XML format that is understood by a variety of other tools. This means gcovr is limited to that XML schema, and cannot include extra information.

Gcovr's HTML reports (--html-details) display branch coverage. To see which branches are uncovered, it is often easiest to see which statements in the conditional branches are uncovered. However, the branch coverage column also displays small icons that indicate which branches were taken. There is one indicator per branch / two per condition. A green ✔ indicates a covered branch, a red ✘ an uncovered branch.

screenshot of branch indicators in a gcovr HTML report

In the above example, the first branch is uncovered. Whether the first branch corresponds to a true or false condition is not defined. If in doubt, rewrite your code to only use statement-level conditionals (no … ? … : …, &&, || operators) so that all branches are separate statements. Note that in C++, exception handling can introduce additional branches that may be difficult/impossible to cover.

Tip: the --branch option is unnecessary to get branch coverage. It only controls whether the gcovr text report shows branch or line coverage.

amon
  • 57,091
  • 2
  • 89
  • 149
  • So there is no way to get the information from the Gcovr's xml then. Parsing the html to use it on my program will be hard. – AceVez Mar 15 '18 at 18:45
  • At that point you may prefer to parse the machine-readable gcov output directly. The gcovr html report is intended for human consumption, and the layout will change in the future. – amon Mar 15 '18 at 18:59
  • I'm sorry if this is the wrong place to ask this but is it possible to modify Gcovr's python script directly to add a line or two on the xml? my knowledge on Python is beginner at best so currently I'm still scanning around to see if there is anything that I can tweak. – AceVez Mar 16 '18 at 04:02
  • How will you be using the XML? If you change the structure, it no longer conforms to the Cobertura format. If you want to dive into the code, get the development version from GitHub and look at `gcovr/cobertura_xml_generator.py`. Currently, the reported condition coverage is aggregated from all branches on that line. – amon Mar 16 '18 at 09:26
  • This picture in example is really artificial. When you have one condition and true/false, it's easy. But I have two conditions (`a || b`) and 4 branches. And here it gets tricky - I have no idea which branch is not covered, and line coverage is 100% – The Godfather Jan 23 '20 at 14:08