JaCoCo - SonarQube - No information about coverage per test

匿名 (未验证) 提交于 2019-12-03 03:04:01

问题:

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.

回答1:

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



回答2:

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 depricated by sonar.

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


回答3:

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.

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



回答4:

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.



回答5:

Make sure to split up build and analysis to two different build steps:

mvn clean install  mvn sonar:sonar 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!