Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

There are two different ways to feed SonarQube with information on unit tests execution and code coverage:

  • Reusing existing reports that have been previously generated by external tools.
  • Use its embedded engine to let SonarQube launch the unit test and code coverage execution directly. Then, the analyzer will automatically feed the database with data exported from the generated reports. (Maven only)Reusing existing reports that have been previously generated by external tools.This mode will be deprecated starting at version 2.2 of the Java plugin.

Project samples covering the different cases below are available on github that can be browsed or downloadedprojects/languages/java/code-coverage/

See Component Viewer on Unit Test File or Quality Flows > Lack of Unit Tests to browse the results in the web interface.

Reusing Existing Reports

Every analyzer supports the reuse reports mode.

To be reused by SonarQube:

  • The tests execution reports have to comply to the JUnit XML format.
  • The code coverage reports have to be generated by JaCoCoCobertura or Clover.

First, tell SonarQube to reuse existing reports:

With Maven in your pom.xml file:

Code Block
languagehtml/xml
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>

With Ant in your build.xml file:

Code Block
languagehtml/xml
<property name="sonar.dynamicAnalysis" value="reuseReports" />

With SonarQube Runner in your sonar-project.properties file:

Code Block
languagehtml/xml
sonar.dynamicAnalysis=reuseReports

Test Execution Reports

Tell SonarQube where your unit tests execution reports are: absolute or relative path to the directory containing your reports

With Maven in your pom file:

Code Block
languagehtml/xml
<sonar.junit.reportsPath>[baseDir]/myReports/myExecutionReports</sonar.junit.reportsPath>

With Ant in your build file:

Code Block
languagehtml/xml
<property name="sonar.junit.reportsPath" value="[baseDir]/myReports/myExecutionReports" />

With SonarQube Runner in your sonar-project.properties file:

Code Block
languagehtml/xml
sonar.junit.reportsPath=[baseDir]/myReports/myExecutionReports

Code Coverage Report 

First, tell SonarQube which code coverage engine has been used to generate the reports: jacoco, cobertura or clover.

Code Block
languagehtml/xml
<sonar.java.coveragePlugin>jacoco | cobertura | clover</sonar.java.coveragePlugin>

Then, tell SonarQube where to get the code coverage reports.

With Maven in your pom file:

Code Block
languagehtml/xml
<sonar.jacoco.reportPath>[baseDir]/myReports/myCodeCoverageReport</sonar.jacoco.reportPath>

With Ant in your build file:

Code Block
languagehtml/xml
<property name="sonar.jacoco.reportPath" value="[baseDir]/myReports/myCodeCoverageReport" />

With SonarQube Runner in your sonar-project.properties file:

Code Block
languagehtml/xml
sonar.jacoco.reportPath=[baseDir]/myReports/myCodeCoverageReport

Depending on the code coverage tool you used to generate your report, replace jacoco by cobertura or clover in the property name.

Using SonarQube Embedded Engine

...

Sample projects are available on github that can be browsed or downloaded: projects projects/code-coverage/combined ut-it/maven/combined-ut-it-multimodule-maven-jacoco

Note that this project structure is not compatible with the below feature: mapping of unit tests and covered code.

Anchor
mappingUnitTest
mappingUnitTest

...

Mapping of Unit Tests and Covered Code

Since SonarQube 3.5, it is possible to display the mapping of unit tests and covered code. The objective is to answer the following questions:

...

This feature is only available while using JaCoCo as the code coverage tool.

Reusing Existing Reports

Every analyzer supports the reuse reports mode.

To be reused by SonarQube:

  • The tests execution reports have to comply to the JUnit XML format.
    The code coverage reports have to be generated by JaCoCo, Cobertura or Clover.

Sample projects are available on github that can be browsed or downloaded:

  • SonarQube Runner and JaCoCo: projects/code-coverage/ut/sonar-runner/ut-sonarRunner-jacoco-reuseReports
  • Maven and JaCoCo: projects/code-coverage/ut/maven/ut-maven-jacoco-reuseReports
  • Ant and JaCoCo: projects/code-coverage/ut/ant/ut-ant-jacoco-reuseReports
  • Ant and JaCoCo (JaCoCo report generated with the JaCoCo Ant Task): projects/code-coverage/ut/ant/ut-ant-jacoco-runTests
  • Ant and JaCoCo, multi-module project (JaCoCo report generated with the JaCoCo Ant Task): projects/code-coverage/ut/ant/ut-ant-jacoco-modules-runTests

First, tell SonarQube to reuse existing reports:

With Maven in your pom.xml file:

Code Block
languagehtml/xml
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>

With Ant in your build.xml file:

Code Block
languagehtml/xml
<property name="sonar.dynamicAnalysis" value="reuseReports" />

With SonarQube Runner in your sonar-project.properties file:

Code Block
languagehtml/xml
sonar.dynamicAnalysis=reuseReports

Test Execution Reports

Tell SonarQube where your unit tests execution reports are: absolute or relative path to the directory containing your reports

With Maven in your pom file:

Code Block
languagehtml/xml
<sonar.junit.reportsPath>[baseDir]/myReports/myExecutionReports</sonar.junit.reportsPath>

With Ant in your build file:

Code Block
languagehtml/xml
<property name="sonar.junit.reportsPath" value="[baseDir]/myReports/myExecutionReports" />

With SonarQube Runner in your sonar-project.properties file:

Code Block
languagehtml/xml
sonar.junit.reportsPath=[baseDir]/myReports/myExecutionReports

Code Coverage Report 

First, tell SonarQube which code coverage engine has been used to generate the reports: jacoco, cobertura or clover.

Code Block
languagehtml/xml
<sonar.java.coveragePlugin>jacoco | cobertura | clover</sonar.java.coveragePlugin>

Then, tell SonarQube where to get the code coverage reports.

With Maven in your pom file:

Code Block
languagehtml/xml
<sonar.jacoco.reportPath>[baseDir]/myReports/myCodeCoverageReport</sonar.jacoco.reportPath>

With Ant in your build file:

Code Block
languagehtml/xml
<property name="sonar.jacoco.reportPath" value="[baseDir]/myReports/myCodeCoverageReport" />

With SonarQube Runner in your sonar-project.properties file:

Code Block
languagehtml/xml
sonar.jacoco.reportPath=[baseDir]/myReports/myCodeCoverageReport

Depending on the code coverage tool you used to generate your report, replace jacoco by cobertura or clover in the property name.

FAQ

0% code coverage reported, but unit tests are correctly executed

...