21
votes

I'm using JaCoCo for Code Coverage. The Unit Test reports are created with junit and they are imported correctly, so that the unit test information is shown properly. The problem is, that I get the error message: No information about coverage per test. and the code coverage is shows the value 0% for unit tests, integration tests and overall coverage. I checked all required information in the sonar-project.properties like binary, src, tests etc.

I'm using:
- SonarQube 4.5.1
- SonarRunner 2.4
- MySQL
- junit 4.1.1
- jacoco 0.7.2

The jacoco.exec is located in a file /target in the project base directory.

Following you can see the sonar-project.properties: From my point of view all necessary paths are set properly. (i.e. binary, src, tests)

Comma-separated paths to directories with sources (required)
sonar.sources=src

compiled code
sonar.java.binaries=class

source code of unit tests 
sonar.tests=test/src

Comma-separated paths to files with third-party libraries (JAR files in the case of Java)
sonar.java.libraries=jar

Language
sonar.language=java

Encoding of the source files
sonar.sourceEncoding=UTF-8

Additional parameters
sonar.my.property=value

Set Project Base
sonar.projectBaseDir=C:/snapshots/steffen_latest/software/java

Tells SonarQube to reuse existing reports for unit tests execution and coverage reports
sonar.dynamicAnalysis=reuseReports

JUnit path
sonar.surefire.reportsPath=test/report/junit

Tells SonarQube where the unit tests execution reports are
sonar.junit.reportsPath=test/report/junit

Tells SonarQube that the code coverage tool by unit tests is JaCoCo
sonar.java.coveragePlugin=jacoco

Import JaCoCo code coverage report.
Tells SonarQube where the unit tests code coverage report is
Unit Tests Coverage
sonar.jacoco.reportPath=target/jacoco.exec

Tells SonarQube where the integration tests code coverage report is
sonar.jacoco.itReportPath=target/it-jacoco.exec

This is the logging file from sonar-runner:

13:56:05.883 INFO  - Sensor SurefireSensor...
13:56:05.883 INFO  - parsing C:\work\snapshots\steffen_latest\software\java\test\report\junit
13:56:06.149 INFO  - Sensor SurefireSensor done: 266 ms
13:56:06.149 INFO  - Sensor JaCoCoItSensor...
13:56:06.195 INFO  - Analysing C:\work\snapshots\steffen_latest\software\java\target\it-jacoco.exec
13:56:06.726 INFO  - **No information about coverage per test**.
13:56:06.726 INFO  - Sensor JaCoCoItSensor done: 577 ms
13:56:06.726 INFO  - Sensor JaCoCoOverallSensor...
13:56:06.851 INFO  - Analysing C:\work\snapshots\steffen_latest\software\java\.sonar\jacoco-overall.exec
13:56:07.178 INFO  - **No information about coverage per test**.
13:56:07.178 INFO  - Sensor JaCoCoOverallSensor done: 452 ms
13:56:07.178 INFO  - Sensor JaCoCoSensor...
13:56:07.209 INFO  - Analysing C:\work\snapshots\steffen_latest\or_base\software\java\target\jacoco.exec
13:56:07.521 INFO  - **No information about coverage per test**.
13:56:07.521 INFO  - Sensor JaCoCoSensor done: 343 ms
13:56:07.521 INFO  - Sensor CPD Sensor (wrapped)...
13:56:07.521 INFO  - JavaCpdEngine is used for java
13:56:07.521 INFO  - Cross-project analysis disabled
13:56:09.019 INFO  - Sensor CPD Sensor (wrapped) done: 1498 ms
13:56:09.144 INFO  - Execute decorators...
13:56:16.166 INFO  - Store results in database

Could anyone give me an advice what could be the problem? Since I don't know what is the problem... I'm working on this issues since a few days and I really don't know what to do..

Thank you in advance.

4
coverage per test is an information on top of coverage. Coverage per test is the information about which test covered which file. (whereas coverage only inform you about what lines have been covered by tests). So please clarify your question based on that information, as the ´No information about coverage per test.´ message only inform us that you have not used any listener to dump jacoco sessions between each tests.benzonico
Thanks for the hint. The problem was that we didn't use any listener to dump jacoco sessions between each test.Stelos10
Any chance on posting how you configured that? I have the same problemNicolas Mommaerts
@Stelos10 please post how you configured the listener to dump jacoco sessions between each test...TheDevOpsGuru
To get coverage per test the Sonar documentation points to a sample project where they use Maven profile to enable with special JUnit listener to enable this functionality. Check here.zloster

4 Answers

2
votes

Have you tried using the prepare-agent?

mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install

Also, if your coverage keeps showing 0%, you might need to follow this advice:

If your project already uses the argLine to configure the surefire-maven-plugin, be sure that argLine defined as a property, rather than as part of the plugin configuration."

1
votes

With this Maven configuration I am able to see coverage per test data.

You need to configure sonar-jacoco-listeners to get coverage per test.

Please be aware that it is deprecated by sonar: "this feature is deprecated at SonarQube level and will no longer receive further improvements/maintenance."

    <skipTests>false</skipTests>
            <!--Jacoco settings -->
            <jacoco.haltOnFailure>false</jacoco.haltOnFailure>
            <jacoco.skip>false</jacoco.skip>

            <!-- sonar JACOCO properties -->
            <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
            <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
            <sonar.jacoco.reportPaths>${project.reporting.outputDirectory}/jacoco-ut.exec</sonar.jacoco.reportPaths>
            <sonar.language>java</sonar.language>


<!-- Added for Jacoco -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                  <properties>
                    <property>
                      <name>listener</name>
                      <value>org.sonar.java.jacoco.JUnitListener</value>
                    </property>
                  </properties>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.1</version>
                <configuration>
                    <destFile>${sonar.jacoco.reportPaths}</destFile>
                    <append>true</append>
                </configuration>
                <executions>
                    <execution>
                        <id>agent</id>
                        <goals>
                          <goal>prepare-agent</goal>
                        </goals>
                      </execution>
                </executions>
            </plugin>



        <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>org.jacoco.agent</artifactId>
            <version>0.8.1</version>
            <classifier>runtime</classifier>
        </dependency>
        <dependency>
          <groupId>org.codehaus.sonar-plugins.java</groupId>
          <artifactId>sonar-jacoco-listeners</artifactId>
          <version>1.2</version>
          <scope>test</scope>
        </dependency>
0
votes

In my case below commands works.

mvn clean org.jacoco:jacoco-maven-plugin:0.7.3.201502191951:prepare-agent install
mvn sonar:sonar 

To check code coverage: Start SonarQube server -> Run above two commands one after another & you will see code coverage in SonarQube Client.

SonarQube Code Coverage

FYI: My SonarQube Version - 5.1.2. You can download latest version from SonarQube Download

0
votes

I use JUnit as well and in my case the issue was because of having TestNG dependency in my pom.xml. After removing this unnecessary dependency, everything started to work as expected.