...
There are two different ways to feed Sonar SonarQube with information on unit tests execution and code coverage:
- Using its embedded engine to let Sonar SonarQube directly launch the unit tests execution and code coverage. Then, the analyzer will automatically fed the database with data exported from the generated reports.
- Reusing existing reports that have been previously generated by external tools.
...
...
Using SonarQube Embedded Engine
Maven supports this mode with the following code coverage tools: JaCoCo, Cobertura, Emma and Clover.
The Sonar SonarQube Ant Task and the Sonar SonarQube Runner do not support this mode.
| Note | ||
|---|---|---|
| ||
Before running a Sonar an analysis with JaCoCo set as the code coverage tool, the source code has to be compiled. |
First, tell Sonar SonarQube to execute the unit tests and the code coverage tool:
...
With Maven, then, you just have to tell Sonar SonarQube which code coverage engine you wish to use: jacoco or cobertura or emma or clover.
| Code Block | ||
|---|---|---|
| ||
<sonar.java.coveragePlugin>jacoco or cobertura or emma or clover</sonar.java.coveragePlugin> Prior to SonarSonarQube 3.4, the property was sonar.core.codeCoveragePlugin. |
Note that this property can be set at Sonar SonarQube instance level through the web interface: Settings > Configuration > General Settings > Java.
...
Sample projects are available on github that can be browsed or downloaded: 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.
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:
- Which files are covered by a given unit test?
- How many lines of code are covered by a given unit test?
- Which lines are covered by a given unit test?
- Which tests do cover a given line of code?
A sample project is available on github that can be browsed or downloaded: projects/code-coverage/ut/maven/ut-maven-jacoco-runTests. What is necessary to get this mapping is contained between 'BEGIN/END: Specific to mapping unit tests and covered code' tags in the pom file. Note that you have to run the analysis with the 'coverage-per-test' profile. Read the README file for more information.
Requires:
- JUnit 4.7+
- Maven Surefire Plugin 2.4+
Reusing Existing Reports
Every analyzer supports this reusing reports mode.
To be reused by Sonar SonarQube :
- The tests execution reports have to comply to the JUnit XML format.
- The code coverage reports have to be generated by either JaCoCo, Emma, Cobertura or Clover.
- Sonar SonarQube Runner and JaCoCo: projects projects/code-coverage/ut/sonar-runner/ut-sonarRunner-jacoco-reuseReports
- Maven and JaCoCo: projects/code-coverage/ut/maven/ut-maven-jacoco-reuseReports
- Maven and Emma: projects/code-coverage/ut/maven/ut-maven-emma-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 projects/code-coverage/ut/ant/ut-ant-jacoco-runTests
- Ant and JaCoCo, multi-module project (JaCoCo report generated with the JaCoCo Ant Task): projects projects/code-coverage/ut/ant/ut-ant-jacoco-modules-runTests
First, tell Sonar SonarQube to reuse existing reports:
...
| Code Block | ||
|---|---|---|
| ||
<property name="sonar.dynamicAnalysis" value="reuseReports" /> |
With Sonar Runner SonarQubeRunner in your sonar-project.properties file:
| Code Block | ||
|---|---|---|
| ||
sonar.dynamicAnalysis=reuseReports |
Test Execution Reports
Tell Sonar SonarQube where your unit tests execution reports are: absolute or relative path to the directory containing your reports
...
| Code Block | ||
|---|---|---|
| ||
<property name="sonar.surefire.reportsPath" value="[baseDir]/myReports/myExecutionReports" /> |
With Sonar SonarQube Runner in your sonar-project.properties file:
| Code Block | ||
|---|---|---|
| ||
sonar.surefire.reportsPath=[baseDir]/myReports/myExecutionReports |
Code Coverage Report
First, tell Sonar SonarQube which code coverage engine has been used to generate the reports: jacoco or cobertura or emma or clover.
| Code Block | ||
|---|---|---|
| ||
<sonar.java.coveragePlugin>jacoco or cobertura or emma or clover</sonar.java.coveragePlugin> Prior to SonarSonarQube 3.4, the property was sonar.core.codeCoveragePlugin. |
Then, tell Sonar SonarQube where to get the code coverage reports:
...
| Code Block | ||
|---|---|---|
| ||
<property name="sonar.jacoco.reportPath" value="[baseDir]/myReports/myCodeCoverageReport" /> |
With Sonar Runner SonarQubeRunner in your sonar-project.properties file:
| Code Block | ||
|---|---|---|
| ||
sonar.jacoco.reportPath=[baseDir]/myReports/myCodeCoverageReport |
Depending on the code coverage tool you used to generate your report, replace jacoco by emma, cobertura or clover in the property name.
See also the Manage Code Coverage by Unit Tests with Sonar blog post.
FAQ
0% code coverage reported whereas unit tests are correctly executed
This problem occurs while using the Maven Cobertura Plugin and a special configuration of the Maven Surefire Plugin preventing unit tests to be forked. This problem can be solved by removing the line "<forkMode>never</forkMode>" in the Maven configuration file (see SONAR-1445 and MCOBERTURA-70).
java.lang.Error: Unable to access JaCoCo Agent - make sure that you use JaCoCo and version not lower than 0.6.2
- Make sure that you do NOT set 'forkMode' to 'never' on the Surefire plugin - otherwise it will not fork the process and no JaCoCo agent will be attached.
| Code Block | ||||
|---|---|---|---|---|
| ||||
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<forkMode>never</forkMode> <!-- Do not set forkMode to never -->
<reportFormat>xml</reportFormat>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
... |
- Make sure that you call first
mvn clean installand thenmvn sonar:sonar Pcoverage-per-testseparately. Indeed, only SonarQube can attach the JaCoCo agent, somvn clean install sonar:sonar -Pcoverage-per-testwill produce the error because thetestphase will be executed a first time alone.

