There are two different ways to feed SonarQube with information on unit tests execution and code coverage:
- 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.
Using SonarQube Embedded Engine
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:
Add this to your pom file:
- 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
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
- 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 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 you need to add to your pom.xml to get this 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.
- JUnit 4.7+
- Maven Surefire Plugin 2.4+
This feature is only available while using JaCoCo as the code coverage tool.
Reusing Existing Reports
Every analyzer supports the reuse reports mode.
- The tests execution reports have to comply to the JUnit XML format.
- The code coverage reports have to be generated by JaCoCo, Emma, Cobertura or Clover.
- 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
- 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/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
<sonar.java.coveragePlugin>jacoco | cobertura | emma | 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:.
With Maven in your pom file:
Depending on the code coverage tool you used to generate your report, replace jacoco by emma, cobertura cobertura or clover in the property name.
0% code coverage reported, but unit tests are correctly executed