Coverage exclusion not working as expected

Hi,

I am using sonarcloud analysis in my java project with gradle. I want to exclude some files/folders from codecoverage (dtos, etc.). In gradle I defined following exlusion:

property "sonar.coverage.exclusions", "'**/config', '**/configuration', '**/dtos', '**/*Config*', '**/*Configuration*','**/*Dto*'"

During CI/CD build I see in logs that this option was picked:

Indexing files...
Project configuration:
  Excluded sources for coverage: '**/config', '**/configuration', '**/dtos', '**/*Config*', '**/*Configuration*', '**/*Dto*'

But when I open project in sonarcloud, not all files/folders are excluded from code coverage analysis. As you could see in screenshot below, only folder dtos was excluded, but based on the definition of coverage exclusions also config and configuration should be excluded.

Same goes for the content of config folder, based on the definition of coverage exclusions all files inside should be excluded, but for some reason one file isn`t (see screenshot below).

Any suggestions what could be wrong?

Hi @BekyP,

Welcome to the community!

Does the same happen if you leave only **/config in the exclusions (without others '**/configuration', '**dtos' etc.)? You could also try with **/config/** instead of only **/config.

Can you also try without the quotes: ' in the exclusions property?

Best,
Marcin

Hi,

thanks for the response. Removing ' helped, now specified folders/files are removed from code coverage, but reported coverage is still pretty low. Sonarcloud reports 41.9% coverage of lines of code, but jacoco reports 58.9% - I assumed that sonar just picks jacoco .xml reports and shows same coverage as jacoco. Any ideas why the reported numbers are different?

Hi @BekyP,

Coverage is impacted by your exclusions. That is why you receive different values in jacoco and different values in SonarCloud.

Best,
Marcin

Yes that is what I would expected - but with the same exclusions sonar and jacoco gives different coverages and I don`t understand why.

@BekyP,

Did you compare no-exclusions on jacoco level + sonar exclusions vs exclusions on jacoco level + no sonar exclusions?

Best,
Marcin

Yes, that is exactly what I compared. I want to understand why there is such big difference in reported coverages.

You can find out details about how coverage is calculated in SonarCloud here: https://sonarcloud.io/documentation/user-guide/metric-definitions/#tests . Although the data is taken from the Jacoco, Sonarcloud calculates the ‘Coverage’ according to the formula mentioned in the link.

Coverage ( coverage )
It is a mix of Line coverage and Condition coverage. Its goal is to provide an even more accurate answer to the following question: How much of the source code has been covered by the unit tests?

Coverage = (CT + CF + LC)/(2*B + EL)
where

  • CT = conditions that have been evaluated to ‘true’ at least once
  • CF = conditions that have been evaluated to ‘false’ at least once
  • LC = covered lines = lines_to_cover - uncovered_lines
  • B = total number of conditions
  • EL = total number of executable lines ( lines_to_cover )

Thanks for the response. This explains different coverage results.