Rolling up Sonarqube File Code Coverage Stat to Module level

Using SonarQube 7.9.1 but this question applies to other versions.

We have our own definition of “modules”, so I’m downloading the file level stats from sonarqube.
I have at the file level: LoC, coverage percentage

I tried calculating a module’s coverage by doing this: { fileStat → (fileStat.coverage / 100.0) * fileStat.LoC }.sum() / module.LoC

And this gives me a percentage of coverage for the module’s total lines of code.
But this doesn’t match Sonarqube’s calculation if a “component” is a folder; my calculation is consistently low :frowning:

Is there any way to calculate Sonarqube’s folder component level coverage value?
I found this, but don’t think there’s any way to get the conditions covered at a file level basis?

Also may partially be related to this other issue:


Project-level metrics are built up from directory-level metrics which are built up, ultimately, from file-level metrics. As an example.

Maybe that helps?


sorry…should have updated here as well…I made a note in the other thread.

The key is that Sonarqube keeps metrics for
“coverable lines”
“coverable branches/conditionals”
Branches are weighted 2:1 vs. lines in the overall coverage number.
Each file has this info as well as the coverage, so you have to invert this computation for the file, then do the summing, then do the calculation by module, etc.


Other thread?


yep, this other one: SonarQube calculation coverage - #4 by danieldiaz

sorry to dredge this up, but just to confirm, these are the metrics keys needed to fill that info up?


What’s a bit confusing is that branch_coverage and coverage are percentages.

And if you try to roll these up to the “folder/component” level, do you divide that by the total ncloc+conditions_to_cover?

nvm: found this page and it’s good enough to follow:
On that doc, could elaborate on B a bit and mention all the metricKeys that can be used to do the calculation (was missing one IIRC).


I’ve moved your post in the other thread here.

And it says that you’ve found the metric definition. So is there still an outstanding question?


No. This question can be closed.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.