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:

  • Using Use its embedded engine to let SonarQubedirectly 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. (Maven only)
  • Reusing existing reports that have been previously generated by external tools.

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 on in the web interface.

...

Note
titleCompile

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

 

First, tell SonarQube 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>

...

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

...

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

...

  • 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 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. Read the README file for more information.

...

  • JUnit 4.7+
  • Maven Surefire Plugin 2.4+ 

Reusing Existing Reports

Every analyzer supports this reusing 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 either by JaCoCo, Emma, Cobertura or Clover.
Sample projects are available on github that can be browsed or downloaded:

...

First, tell SonarQube 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 SonarQube where to get the code coverage reports:

...

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

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.

...