Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

Table of Contents

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

  • Using its embedded engine to let Sonar 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.

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

See Resource Viewer on Unit Tests or Quality Flows > Lack of Unit Tests to browse the results on the Sonar web interface.

Using Sonar Embedded Engine

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

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

Compile

Icon

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:

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.

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

First, tell Sonar to reuse existing reports:

With Maven in your pom.xml file:

With Ant in your build.xml file:

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

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:

With Ant in your build file:

With Sonar Runner in your sonar-project file:

Code Coverage Report 

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

 

 

Then, tell Sonar where to get the code coverage reports:

With Maven in your pom file:

With Ant in your build file:

With Sonar Runner in your sonar-project file:

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.

  • No labels