【问题标题】:JaCoCo - SonarQube - No information about coverage per testJaCoCo - SonarQube - 没有关于每次测试覆盖率的信息
【发布时间】:2015-02-02 12:17:50
【问题描述】:

我正在使用 JaCoCo 进行代码覆盖。单元测试报告使用 junit 创建并正确导入,以便正确显示单元测试信息。 问题 是,我收到错误消息: 没有关于每个测试覆盖率的信息。并且代码覆盖率显示单元测试、集成测试和整体覆盖率的值为 0%。 我检查了 sonar-project.properties 中的所有必需信息,例如二进制、src、测试等。

我正在使用:
- SonarQube 4.5.1
- SonarRunner 2.4
- MySQL
- junit 4.1.1
- jacoco 0.7.2

jacoco.exec 位于项目基目录的 /target 文件中。

您可以在下面看到 sonar-project.properties: 从我的角度来看,所有必要的路径都已正确设置。 (即二进制、src、测试)

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

这是来自 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

谁能给我一个建议可能是什么问题? 因为不知道是什么问题... 几天以来我一直在研究这个问题,我真的不知道该怎么办..

提前谢谢你。

【问题讨论】:

  • 每次测试的覆盖率是覆盖率之上的信息。每个测试的覆盖率是关于哪个测试覆盖了哪个文件的信息。 (而覆盖率只通知您测试覆盖了哪些行)。因此,请根据该信息澄清您的问题,因为“没有关于每次测试覆盖率的信息。”消息仅告诉我们您没有使用任何侦听器在每次测试之间转储 jacoco 会话。
  • 感谢您的提示。问题是我们没有使用任何监听器在每次测试之间转储 jacoco 会话。
  • 有机会发布您是如何配置的吗?我有同样的问题
  • @Stelos10 请发布您如何配置侦听器以在每次测试之间转储 jacoco 会话...
  • 要获得coverage per test Sonar documentation 指向一个示例项目,他们在其中使用 Maven 配置文件来启用特殊的 JUnit 侦听器来启用此功能。检查here

标签: sonarqube jacoco


【解决方案1】:

您是否尝试过使用prepare-agent

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

另外,如果您的覆盖率一直显示为 0%,您可能需要关注this advice

如果您的项目已经使用 argLine 来配置 surefire-maven-plugin,请确保将 argLine 定义为属性,而不是作为插件配置的一部分。"

【讨论】:

    【解决方案2】:

    在我的例子中,下面的命令有效。

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

    检查代码覆盖率: 启动 SonarQube 服务器 -> 依次运行以上两个命令,您将在 SonarQube Client 中看到代码覆盖率。

    仅供参考:我的 SonarQube 版本 - 5.1.2。您可以从SonarQube Download下载最新版本

    【讨论】:

      【解决方案3】:

      我也使用 JUnit,在我的情况下,问题是因为我的 pom.xml 中有 TestNG 依赖项。移除这种不必要的依赖后,一切都开始按预期工作了。

      【讨论】:

        【解决方案4】:

        通过这个 Maven 配置,我可以查看每个测试数据的覆盖率。

        您需要配置 sonar-jacoco-listeners 以获得每次测试的覆盖率。

        请注意,sonar 已弃用它:“此功能在 SonarQube 级别已弃用,将不再接受进一步的改进/维护。”

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

        【讨论】:

          猜你喜欢
          • 2018-12-23
          • 2013-05-13
          • 2015-10-15
          • 1970-01-01
          • 2019-10-23
          • 2015-12-10
          • 2019-05-15
          • 2014-04-06
          • 1970-01-01
          相关资源
          最近更新 更多