Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Info
iconfalse
titleTable of Contents

Table of Contents
maxLevel2

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

  • Using Reusing existing reports that have been previously generated by external tools.
  • Use its embedded engine (Deprecated) to let SonarQubeTM directly launch  launch the unit tests execution test and code coverage execution directly. Then, the analyzer will automatically fed feed the database with data exported from the generated reports. Reusing existing reports that have been previously generated by external tools.This mode will be removed 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/

...

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

Maven supports this mode with the following code coverage tools: JaCoCo, Cobertura, Emma and  and Clover.

The SonarQube TM Ant Task and the SonarQube TM Runner do not support this mode.

Note
titleCompile

Before running an analysis with JaCoCo, the source code has to be compiled.

 

First, tell SonarQube TM to execute the unit tests and the code coverage tool:

With Maven in Add this to your pom file:

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

...

With Maven, then, you just have to tell SonarQube TM which code coverage engine you wish to use: jacoco or cobertura or emma or clover.

Code Block
languagehtml/xml
<sonar.java.coveragePlugin>jacoco or| cobertura or| emma or clover</sonar.java.coveragePlugin>
 
Prior to SonarQube 3.4, the property was sonar.core.codeCoveragePlugin.

Note that this property can be set at the SonarQube TM instance level through the web interface: Settings > Configuration > General Settings > Java.

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

  • Maven and JaCoCo: projects/code-coverage/ut/maven/ut-maven-jacoco-runTests
  • Maven and Cobertura: projects/code-coverage/ut/maven/ut-maven-cobertura-runTests

Maven

...

Maven Multi-module Project and JaCoCo

...

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 SonarQubeTM 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?

See 

...

Component Viewer on Unit

...

Test File for more details.

A sample project is available on github that can be browsed or downloaded: projects projects/code-coverage/ut/maven/ut-maven-jacoco-runTests. What is necessary to you need to add to your pom.xml to get this mapping behavior 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 profile. Read the README file for more information.

Requires:

First, tell SonarQubeTM 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 SonarQubeTM Runner in your sonar-project.properties file:

Code Block
languagehtml/xml
sonar.dynamicAnalysis=reuseReports

Test Execution Reports

Tell SonarQubeTM 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.surefire.reportsPath>[baseDir]/myReports/myExecutionReports</sonar.surefire.reportsPath>

With Ant in your build file:

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

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

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

Code Coverage Report 

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

Code Block
languagehtml/xml
<sonar.java.coveragePlugin>jacoco or cobertura or emma or clover</sonar.java.coveragePlugin>
 
Prior to SonarQube 3.4, the property was sonar.core.codeCoveragePlugin.

Then, tell SonarQubeTM 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 SonarQubeTM Runner in your sonar-project.properties file:

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

...

  • JUnit 4.7+
  • Maven Surefire Plugin 2.4+

 

Reusing Existing Reports

Every analyzer supports this reusing reports mode.

To be reused by SonarQubeTM :

  • 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.
Sample projects are available on github that can be browsed or downloaded:

...

  •  

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

FAQ

0% code coverage reported

...

, but unit tests are correctly executed

This problem occurs while using the Maven Cobertura Plugin and a special configuration of the Maven Surefire Plugin preventing prevents the unit tests to be from being forked. This problem can be solved by removing the line "<forkMode>never</forkMode>" in the Maven configuration file (see SONAR-1445 and MCOBERTURA-70).

...

There can be several reasons for which you get this error:
  • 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.

...

  • Make sure that you call first mvn clean install and then mvn sonar:sonar Pcoverage-per-test separately. Indeed, only SonarQube TM can attach the JaCoCo agent, so mvn clean install sonar:sonar -Pcoverage-per-test will produce the error because the test phase will be executed a first time alone.

...