Barun Mishra
Barun Mishra

Reputation: 41

Unable to see Junit cobertura coverage reports in SonarQube

SonarQube is reporting the following for my project.

Unit Tests Coverage 0.0%; Line Coverage 0.0%; Condition Coverage 0.0%

It is not able to find the reports which was created immediately before the Sonar scan. I am using Jenkins to launch the SonarQube scan. In fact in the console output I can see the unit tests being executed and the reports saved in the surefire directory.

Below are the relevant logs from the console output.

[INFO] --- maven-surefire-plugin:2.12:test (default-test) @ earn-promotional-domain ---
[INFO] Surefire report directory: /var/jenkins/workspace/earn/surefire-reports

[INFO] [13:50:20.807] Sensor SurefireSensor
[INFO] [13:50:20.807] parsing /var/jenkins/workspace/earn/surefire-reports
[ERROR] [13:50:20.808] Reports path not found or is not a directory: /var/jenkins/workspace/earn/surefire-reports
[INFO] [13:50:20.808] Sensor SurefireSensor (done) | time=1ms
[INFO] [13:50:20.808] Sensor CoberturaSensor
[INFO] [13:50:20.808] parsing /var/jenkins/workspace/earn/site/cobertura/coverage.xml

I am using SonarQube 5.1.2. Please let me know if any other information is needed that will help to figure out the problem.

Upvotes: 1

Views: 2263

Answers (3)

Bartosz Popiela
Bartosz Popiela

Reputation: 1051

As @Jeel said, you can use an alternative plugin. But if you necessarily must use the cobertura plugin, you can modify it a little bit.

If I understand correctly, the cobertura:check goal forks a current lifecycle, merges a plugin specific configuration (cobertura-maven-plugin-X.X.jar\META-INF\maven\lifecycle.xml) and executes a forked lifecycle to the test phase. After that "check" mojo is executed.

To make cobertura not to fork a lifecycle you can comment <executePhase> tag in a plugin descriptor (cobertura-maven-plugin-X.X.jar\META-INF\maven\plugin.xml) for the check goal. Additionaly, you have to copy the configuration from lifecycle.xml to your build configuration. For version 2.7 there is only surefire configuration:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>${surefire.version}</version>
  <configuration>
    <classesDirectory>${project.build.directory}/generated-classes/cobertura</classesDirectory>
    <testFailureIgnore>false</testFailureIgnore>
  </configuration>
</plugin>

(possibly extending a default lifecycle using components.xml would also work).

In the last step you have to add an execution for the "instrument" goal in the "process-class" phase because it doesn't have a default phase in a plugin descriptor.

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>cobertura-maven-plugin</artifactId>
  <version>${cobertura.version}</version>
  <executions>
    <execution>
      <id>instrument-classes</id>
      <phase>process-classes</phase>
      <goals>
        <goal>instrument</goal>
      </goals>
    </execution>
    <execution>
      <id>check-coverage</id>
      <goals>
        <goal>clean</goal>
        <goal>check</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Upvotes: 0

Kraal
Kraal

Reputation: 2877

If you prefer to stick with Cobertura rather than moving to Jacoco, you can try this alternative to cobertura maven plugin:

https://github.com/QualInsight/qualinsight-mojo-cobertura

Here are some advantages this mojo provides:

  • it runs tests once (instead of twice with cobertura-maven-plugin)
  • it allows you to split coverage (UT / IT / Overall)
  • you get rid of the "obsolete" Cobertura plugin for SonarQube

The documentation on the project's page explains how to add converted reports to SonarQube.

Upvotes: 0

Jeel
Jeel

Reputation: 2545

You are better off with Jacoco than cobertura. Cobertura has lot of open bugs yet to be addressed.

Also Jacoco provides a aggregator plugin which will aggregate the coverage from all child modules and display a comprehensive coverage.

Jacoco also doesnt require any additional plugin for SonarQube.

Here's a good example of implementing Jacoco: http://www.petrikainulainen.net/programming/maven/creating-code-coverage-reports-for-unit-and-integration-tests-with-the-jacoco-maven-plugin/

Upvotes: 1

Related Questions