【问题标题】:Code coverage not reflecting in SonarQube UI代码覆盖率未反映在 SonarQube UI 中
【发布时间】:2021-01-19 04:15:51
【问题描述】:

我正在尝试使用 SonarQube 建立静态代码分析。在 SonarQube UI 中创建了一个项目。在 Jenkins 管道中运行声纳扫描仪。

管道成功运行,报告已发布到 SonarQube,但未按预期显示代码覆盖率:

它表明找到了测试,但仍然显示覆盖率为 0%。

我在 pom.xml 中使用了 jacoco 插件:

   <pluginManagement>
      <plugins>
          <plugin>
          <groupId>org.jacoco</groupId>
          <artifactId>jacoco-maven-plugin</artifactId>
          <version>0.8.6</version>
        </plugin>
      </plugins>
    </pluginManagement> 

我正在管道中运行 jacoco:prepare-agent 和 jacoco:report。甚至在运行 jacoco 目标之前尝试运行 mvn clean install 但没有用。

还要检查在 target/surefire-reports 下生成的 xml 文件,该文件显示测试已运行:


测试集:in.javahome.myweb.controller.CalculatorTest

测试运行:2,失败:0,错误:0,跳过:0,经过时间:0.047 秒

SonarQube 版本:7.9.4 詹金斯版本:2.259

任何关于问题所在的指针将不胜感激。

谢谢

【问题讨论】:

    标签: sonarqube jenkins-pipeline jacoco-maven-plugin


    【解决方案1】:

    这通常意味着您没有将 jacoco 插件与 surefire 插件正确集成。获得代码覆盖率结果的唯一方法是,如果surefire 以能够生成代码覆盖率数据的方式运行。默认情况下它们不会集成在一起。

    这两个插件的重要部分如下:

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco.maven.plugin.version}</version>
                <executions>
                    <!-- Prepares the property pointing to the JaCoCo runtime agent which 
                        is passed as VM argument when Maven the Surefire plugin is executed. -->
                    <execution>
                        <id>pre-unit-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <propertyName>surefireArgLine</propertyName>
                        </configuration>
                    </execution>
    

    还有这个:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.surefire.plugin.version}</version>
                <configuration>
                    <!-- Sets the VM argument line used when unit tests are run. -->
                    <argLine>${surefireArgLine}</argLine>
    

    【讨论】:

    • 能够使用surefire插件生成代码覆盖率。谢谢@David M. Karr
    猜你喜欢
    • 2019-06-30
    • 2020-08-13
    • 1970-01-01
    • 2018-01-11
    • 2016-04-08
    • 2021-06-04
    • 1970-01-01
    • 2017-06-10
    • 2017-10-24
    相关资源
    最近更新 更多