Maybe this is due to my own misunderstanding of what in Sonar is being analyzed, but at the Component level Sonar is reporting a much higher percentage of code coverage than what appears to be possible given the results at the package level. May I point your attention to the attached screenshot... See: https://i.stack.imgur.com/yCnms.png Any ideas?
-
Sonar metrics calculation are explained in the link, check http://docs.codehaus.org/display/SONAR/Metric+definitions – Sajan Chandran Feb 16 '12 at 22:55
-
Hey @SajanChandran I saw this page, but it doesn't explain how code coverage is calculated. – Brett VanderVeen Feb 17 '12 at 14:24
2 Answers
It all depends on the size of the classes and how they contribute to the coverage of the module as a whole
- If Class A had 10 lines of code at 90% coverage then that is 9 lines covered
- If Class B had 1000 lines of code at 60% coverage then that is 600 lines covered
The coverage overall is 60.3% (609/1010)
But if
- If Class A had 10 lines of code at 60% coverage then that is 6 lines covered
- If Class B had 1000 lines of code at 90% coverage then that is 900 lines covered
Now the coverage overall is 89.7% (906/1010)
Then you have to take into account of whether the coverage for filewatcher also includes the coverage metrics of filewatcher.impl - which I suspect it might do.
Finally you haven't mentioned if the number you are displaying is branch/line or a combination of both which the link provided in the comment implies is possible, if the latter then the combinations involved start to make my head hurt.

- 8,228
- 4
- 36
- 56
-
Hey Shaun, thanks for the reply. If you notice... the total coverage when calculated always tends between the two extreme values (lowest and highest). Why, in my situation, is the total coverage so much more than any of the both the package's coverage? Even code branching wouldn't explain what I am seeing. Can you explain the calculation using my situation? – Brett VanderVeen Feb 17 '12 at 14:37
-
[This screenshot](http://i.stack.imgur.com/OQOqJ.png) shows the information necessary to calculate the coverage how you did, but it still doesn't work. First, why aren't all packages displayed on the screen shown in the screenshot on the Question? Second, why does the coverage still not add up? I get ~94.6% when calculated manually, and actually our total coverage went up to 96.1% according to Sonar. – Brett VanderVeen Feb 17 '12 at 15:38
-
The coverage value alone does not just use line coverage (the numbers even rounded to 1 decimal don't make sense unless there is another factor involved) but also t/f branch coverage, but that number is not displayed but as complexity is around 20 then I'd expect the number of branches to be in that approximately double that. As for why coverage not showing perhaps the class has no measurable code or wasn't tested - I only have 1/2 the picture. – Shaun Wilde Feb 18 '12 at 03:27
The screenshot only shows code coverage for packages
com.gfs.transportation.transprointegration.filewatcher
com.gfs.transportation.transprointegration.filewatcher.impl
If the Code coverage for the below components are 100%
com.gfs.transportation.transprointegration
com.gfs.transportation
com.gfs
com
Then total coverage will be (87.9 + 89.2 + 100 + 100 + 100 + 100) /600 which equals 96.183%
I am thinking if a package has no classes under it then the coverage of the package is 100%

- 893
- 1
- 11
- 16

- 345
- 3
- 7
-
Hi Vimil - thanks for that - makes sense the way you explain it but not how I would have expected it – Shaun Wilde Feb 20 '12 at 01:38
-
Here is a [screenshot](http://i.stack.imgur.com/OQOqJ.png) of other packages that are included but weren't on the previous screen. – Brett VanderVeen Feb 21 '12 at 01:10
-
I still don't understand why the other screen in Sonar doesn't show all the packages. – Brett VanderVeen Feb 21 '12 at 01:13