Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Add a note on coverage value when no reports are found
Info
iconfalse
titleTable of Contents

Table of Contents
maxLevel2

...

Overview

It is possible to feed SonarQube with tests execution and code coverage :

...

reports.

...

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

...

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

...

in the

...

web interface.

Using Sonar Embedded Engine

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

...

Usage

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.

You can check the page of each coverage plugin to get more information on how to generate the reports or you can check the Analyzing with Maven documentation.

Note
titleCompile

Before running a Sonar analysis with JaCoCo set as the code coverage tool, the source code has to be compiled.

 

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

With Maven in your pom file:

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

Maven Project

With Maven, then, you just have to tell Sonar 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 Sonar 3.4, the property was sonar.core.codeCoveragePlugin.

Note that this property can be set at Sonar 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 and Emma: projects/code-coverage/ut/maven/ut-maven-emma-runTests

Maven Multi-module Project and JaCoCo

For multi-module Maven projects, compute overall code coverage by unit tests across modules. This case clearly lies between unit tests and integration tests but this is widely used.

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

Reusing Existing Reports

Every analyzer supports this reusing reports mode.

To be reused by Sonar:

  • 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:

...

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

Code Block
languagehtml/xml
sonar.dynamicAnalysis=reuseReports

Test Execution Reports

Tell Sonar 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 Sonar Runner in your sonar-project file:

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

Code Coverage Report 

First, tell Sonar 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 Sonar 3.4, the property was sonar.core.codeCoveragePlugin.

Then, tell Sonar 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 Sonar Runner in your sonar-project 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 emmacobertura or clover in the property name.

 

See also the Manage Code Coverage by Unit Tests with Sonar blog post.

JaCoCo, Surefire and "argLine"

If you use JaCoCo and define argLine in Surefire configuration, please check JaCoCo plugin page for a note on the best usage.

 

When no reports are found, coverage value is not set. This behaviour can be overriden by a property for JaCoCo, please check the JaCoCo plugin page to see how.

 

You can set the following properties to import those reports:

Code Block
languagebash
# Import tests execution reports (JUnit XML format).
# Set the property to the path of the directory containing all the XML reports.
sonar.junit.reportsPath=target/surefire-reports
 
# Import JaCoCo code coverage report.
# Set the property to the path of the JaCoCo .exec report.
sonar.jacoco.reportPath=target/jacoco.exec

# Import Cobertura code coverage report.
# Set the property to the path of the Cobertura .ser report.
sonar.cobertura.reportPath=target/site/cobertura/coverage.xml
 
# Import Clover code coverage report.
# Set the property to the path of the Clover .xml report.
sonar.clover.reportPath=target/clover.xml


# For versions of the Java plugin prior to 2.2, the following properties have to be set as well.
sonar.dynamicAnalysis=reuseReports
sonar.java.coveragePlugin=plugin   # either jacoco or cobertura or clover

Project samples are available on github that can be browsed or downloadedprojects/languages/java/code-coverage/ut