I know what the difference is between line and branch coverage, but what is the difference between code coverage and line coverage? Is the former instruction coverage?
Asked
Active
Viewed 2.4k times
30
-
coverage seems to be a computed metric, that takes line and branch coverage into consideration. Can't find the formula though :D – oers Jul 19 '12 at 13:18
-
hence the question. I think it is instruction coverage as one line of java code can have multiple instructions these two are different. Maybe there will an authoritative answer here ;) – Bartosz Radaczyński Jul 19 '12 at 13:26
-
fabrice from sonar roams this tag, so I think will get some info :) – oers Jul 19 '12 at 13:36
2 Answers
46
Coverage is a subtle ;-) mix of the line and the branch coverage.
You can find the formula on our metric description page:
coverage = (CT + CF + LC)/(2*B + EL)
where
CT - branches that evaluated to "true" at least once
CF - branches that evaluated to "false" at least once
LC - lines covered (lines_to_cover - uncovered_lines)
B - total number of branches (2*B = conditions_to_cover)
EL - total number of executable lines (lines_to_cover)

Julian
- 33,915
- 22
- 119
- 174

Fabrice - SonarSource Team
- 26,535
- 3
- 62
- 58
-
awesome, I was unable to find that page via google... THanks! – Bartosz Radaczyński Jul 19 '12 at 14:24
4
To expand on the answer, you can only query sonar for these terms:
- conditions_to_cover
- uncovered_conditions
- lines_to_cover
- uncovered_lines
And then you can covert to the terms above using these equations:
CT + CF = conditions_to_cover - uncovered_conditions
2*B = conditions_to_cover
LC = lines_to_cover - uncovered_lines
EL = lines_to_cover
You can use the Sonar Drilldown or REST API to get the metric values above:
http://my.sonar.com/drilldown/measures/My-Project-Name?metric=line_coverage
http://my.sonar.com/api/resources?resource=55555&metrics=ncloc,conditions_to_cover,uncovered_conditions,lines_to_cover,uncovered_lines,coverage,line_coverage,branch_coverage,it_conditions_to_cover,it_uncovered_conditions,it_lines_to_cover,it_uncovered_lines,it_coverage,it_line_coverage,it_branch_coverage,overall_conditions_to_cover,overall_uncovered_conditions,overall_lines_to_cover,overall_uncovered_lines,overall_coverage,overall_line_coverage,overall_branch_coverage
This blog post has additional information: http://sizustech.blogspot.com/2015/10/making-sense-of-sonar-qube-stats-like.html

Scott Izu
- 2,229
- 25
- 12
-
It was very unclear that CT + CF in fact conditions_to_cover - uncovered_conditions and 2*B = conditions_to_cover, thanks for highlighting! – Roman Badiornyi Jan 18 '21 at 15:40