Code Coverage is always 0.0%, using 8.3 version

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
    8.3 Sonarqube Enterprise

  • what are you trying to achieve
    Pull request analysis integrating GitHub

  • what have you tried so far to achieve this
    We are able to decorate the pull request and see the code change on sonar dashboard.

But failed because of code coverage is always 0% even after few lines of code change in PR,

We are configures sonar analysis for multi languages(java, sql, python) in repo.

sonar-scanner -Dsonar.sourceEncoding=UTF-8 \
              -Dsonar.projectKey=reponame \
              -Dsonar.host.url=https://sonarqube.com/sonar/ \
              -Dsonar.login=$SONAR_TOKEN \
              -Dsonar.java.binaries=. \
              -Dsonar.java.libraries=. \
              -Dsonar.pullrequest.key=GIT_PR_ID \
              -Dsonar.pullrequest.branch=GIT_PR_SOURCE_BRANCH \
              -Dsonar.pullrequest.base=GIT_PR_TARGET_BRANCH \
              -Dsonar.pullrequest.github.repository=repo/path \
              -Dsonar.scm.revision=GIT_PR_COMMIT
## QUALITY GATE STATUS

Conditions on New Code

### Failed

## FAILED CONDITION
0.0%
Coverage on New Codeis less than 80.0%

## MEASURES

[0]New Bugs

Reliability[A]

[0]New Vulnerabilities

Security[A]

[0]

Security Review[A]

[9]New Code Smells

Maintainability[A]

[0.0%]Coverage on [50] New Lines to cover

0.0%Estimated after merge

[0.0%]Duplications on [62]New Lines

Please suggest.

Hi,

I don’t see in your command line where you’re passing the coverage report in. Here’s the list of available parameters for that.

 
Ann

Hi @ganncamp Thanks for your reply.

Actually here quality gate is failing because Coverage on New Code is less than 80.0%

But same quality gate is passed even the code coverage is 0% when its Coverage on 0 New Lines to cover.

Seems its failing as the new lines of code (new file ) is not analysing…

I have tried adding “-Dsonar.coverage.jacoco.xmlReportPaths” in command line

But no luck…Please help on this…

Thanks.

Hi,

Right. If there are no new lines to cover then… they don’t need to be covered. In fact, you’ll see this behavior for up to 20 new lines of code; a coverage on new code condition in your Quality Gate will only kick in when you have >20 new lines of code.

You’ve added the key in what needs to be a key/value pair. It should be -Dsonar.coverage.jacoco.xmlReportPaths=[path to report].

 
HTH,
Ann

Hi @ganncamp

Here, In the repo we have updated a new python file, which consists 50-60 lines of code. which is >20 new lines of code.

we got the below values after sonar scan the PR.

Coverage on New Code is 0.0%,
Lines to Cover is 50
Uncovered Lines on New Code is 50
UncoveredConditions on New Code is 0.

FAILED CONDITIONS

Coverage on New Code is less than 80.0%, so quality gate failed.

sonar-scanner -Dsonar.sourceEncoding=UTF-8
-Dsonar.projectKey=reponame
-Dsonar.host.url=https://sonarqube.com/sonar/
-Dsonar.login=$SONAR_TOKEN
-Dsonar.java.binaries=.
-Dsonar.java.libraries=.
-Dsonar.pullrequest.key=GIT_PR_ID
-Dsonar.pullrequest.branch=GIT_PR_SOURCE_BRANCH
-Dsonar.pullrequest.base=GIT_PR_TARGET_BRANCH
-Dsonar.pullrequest.github.repository=repo/path
-Dsonar.scm.revision=GIT_PR_COMMIT

Please suggest on how to over come this.
and if any sonar property needs to add to include the new lines of code to scan in PR.

Hi,

Again, I’m not seeing where in your analysis parameters you’re passing in a test report. To be clear, SonarQube analysis

  • does not execute your tests for you
  • does not generate a coverage report for you
  • does not automatically read the coverage report you generate unless you tell it to. And from what I can see, you’re not telling it to.

Again,

 
HTH,
Ann

Hi @ganncamp, Still its not working for us.

NOTE:we are trying sonar analysis for pull requests using sonar-scanner.

 sonar-scanner -Dsonar.sourceEncoding=UTF-8 \
               -Dsonar.projectKey=reponame \
               -Dsonar.host.url=https://sonarqube.com/sonar/ \
               -Dsonar.login=$SONAR_TOKEN \
               -Dsonar.java.binaries=. \
               -Dsonar.sources=. \
               -Dsonar.java.libraries=. \
               -Dsonar.pullrequest.key=GIT_PR_ID \
               -Dsonar.pullrequest.branch=GIT_PR_SOURCE_BRANCH \
               -Dsonar.pullrequest.base=GIT_PR_TARGET_BRANCH \
               -Dsonar.pullrequest.github.repository=repo/path \
               -Dsonar.scm.revision=GIT_PR_COMMIT
               -Dsonar.coverage.jacoco.xmlReportPaths=${WORKSPACE}/report.xml \
               -Dsonar.verbose=true

It should be -Dsonar.coverage.jacoco.xmlReportPaths=[path to report] .

I am not sure about the [path to report]` given in properties, Could you please explain me how its works and the process of sonar.coverage.jacoco. property.

Do we need any dependency needed on pom.xml or any other places for coverage Jacoco.

PFA screenshots
In the quality gate condition for coverage is greater than 80, Since no new lines are covered in the Coverage, its shows 0. Hence, Quality gate is failed.


Please help on this.

Thanks.

Hi,

Actually, it looks like you’re almost there. Generally, when you provide paths for analysis parameters, you’re going to provide paths from project root so probably not

-Dsonar.coverage.jacoco.xmlReportPaths=${WORKSPACE}/report.xml \

but

-Dsonar.coverage.jacoco.xmlReportPaths=report.xml \

assuming your report really is in your project root.

That’s actually a question of generating your analysis report, which is not my area of expertise. But I don’t believe you need to modify your pom for this.

 
HTH,
Ann

@ganncamp, In our observation, as we found the jacoco report.xml is not generating in PR scan.

Please let us know, how can we generate jacoco report xml file using sonar-scanner only for PR.

Once report xml file is generated during PR scan build, we can pass the path to report.
-Dsonar.coverage.jacoco.xmlReportPaths=[path to report].

Thanks.

Hi,

That’s a different problem, and one external to SonarQube. In general, you should take the same steps to generate this report for PRs that you do for analysis of the main branch.

 
HTH,
Ann