【问题标题】:Unable to see Junit cobertura coverage reports in SonarQube无法在 SonarQube 中查看 Junit cobertura 覆盖率报告
【发布时间】:2016-07-12 11:09:36
【问题描述】:

SonarQube 正在为我的项目报告以下内容。

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

它无法找到在声纳扫描之前创建的报告。我正在使用 Jenkins 启动 SonarQube 扫描。事实上,在控制台输出中,我可以看到正在执行的单元测试和保存在 surefire 目录中的报告。

以下是控制台输出的相关日志。

[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

我正在使用 SonarQube 5.1.2。如果需要任何其他有助于找出问题的信息,请告诉我。

【问题讨论】:

  • 我使用 JaCoCo,但过程应该是相同的(ish)。您需要注入该仪器类的代理 (mojohaus.org/cobertura-maven-plugin) 然后您需要触发 sonarQube 分析。使用 cobertura 的结果(重用报告)或再次运行测试。在这里,我为 JaCoCo -Dsonar.binaries=target/classes -Dsonar.junit.reportsPath=target/surefire-reports -Dsonar.jacoco.reportPath=target/coverage-reports/jacoco-ut.exec
  • 谢谢@drgn!我没有意识到我需要将目标目录作为参数提供,因为我有一个不同的项目,我不必提供目标目录。我能看到的唯一区别是,在不工作的项目中,有很多子项目,因此有很多子目录。您认为我必须将每个文件夹作为声纳报告路径吗?
  • 这些子项目是maven项目,有自己的pom.xml吗?
  • 是的。顶层有一个父 pom,但每个子项目都有自己的 pom 文件。

标签: maven jenkins junit sonarqube cobertura


【解决方案1】:

Jacoco 比 cobertura 更好。 Cobertura 有许多尚未解决的未解决的错误。

Jacoco 还提供了一个聚合器插件,它将聚合所有子模块的覆盖率并显示全面的覆盖率。

Jacoco 也不需要任何额外的 SonarQube 插件。

这是一个实现 Jacoco 的好例子: http://www.petrikainulainen.net/programming/maven/creating-code-coverage-reports-for-unit-and-integration-tests-with-the-jacoco-maven-plugin/

【讨论】:

  • 谢谢杰尔!我也倾向于这个选项。我会试试Jacoco,看看我是否有更好的运气!
【解决方案2】:

如果您更喜欢坚持使用 Cobertura 而不是迁移到 Jacoco,您可以尝试使用这个 cobertura maven 插件的替代方案:

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

以下是这个 mojo 提供的一些优势:

  • 它运行一次测试(而不是使用 cobertura-maven-plugin 运行两次)
  • 它允许您拆分覆盖范围(UT / IT / 整体)
  • 您摆脱了 SonarQube 的“过时”Cobertura 插件

项目页面上的文档说明了如何将转换后的报告添加到 SonarQube。

【讨论】:

    【解决方案3】:

    正如@Jeel 所说,您可以使用替代插件。但是如果你一定要使用cobertura插件,你可以稍微修改一下。

    如果我理解正确,cobertura:check 目标分叉当前生命周期,合并插件特定配置 (cobertura-maven-plugin-XXjar\META-INF\maven\lifecycle.xml) 并执行分叉生命周期到测试阶段。在执行“检查” mojo 之后。

    要使 cobertura 不分叉生命周期,您可以在插件描述符 (cobertura-maven-plugin-X.X.jar\META-INF\maven\plugin.xml) 中为检查目标添加 标记。 另外,您必须将配置从生命周期.xml 复制到您的构建配置。对于 2.7 版,只有万无一失的配置:

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

    (也可以使用components.xml 扩展默认生命周期)。

    在最后一步中,您必须在“process-class”阶段为“instrument”目标添加一个执行,因为它在插件描述符中没有默认阶段。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      • 2014-08-08
      • 2015-07-15
      • 1970-01-01
      • 2017-01-03
      • 2010-11-14
      • 1970-01-01
      相关资源
      最近更新 更多