gpt4 book ai didi

java - maven jacoco 插件不生成覆盖率报告

转载 作者:行者123 更新时间:2023-11-30 08:39:19 25 4
gpt4 key购买 nike

我使用 sonarqube 作为测试结果的输出,而使用 maven 和 jacoco 来测试测试用例。

Sonarqube 版本是 5.4Maven 版本是 3.3.9Jacoco 版本 0.7

这是我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.sonarqube</groupId>
<artifactId>example-ut-maven-jacoco</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Java :: UT Coverage with JaCoCo :: Maven</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!-- Minimal supported version is 4.7 -->
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</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>
<!-- Sets the path to the file which contains the execution data. -->
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
<!--
Sets the name of the property containing the settings
for JaCoCo runtime agent.
-->
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<!--
Ensures that the code coverage report for unit tests is created after
unit tests have been run.
-->
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
<configuration>
<!-- Sets the VM argument line used when unit tests are run. -->
<argLine>${surefireArgLine}</argLine>
<!-- Skips unit tests if the value of skip.unit.tests property is true -->
<skipTests>${skip.unit.tests}</skipTests>
</configuration>
</plugin>
</plugins>
</build>



</project>

我也在关注这个link here但对于我在这里使用的文件 enter link description here .

这是我构建测试项目的方式

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

构建成功返回,但没有生成代码覆盖率我还通过转到 sonarqube localhost:9000/进行了检查,它显示 0.00% 的代码覆盖率,但单元测试都显示为成功“成功”。

知道是什么导致了这个问题吗?而且我对这一切还是陌生的。

最佳答案

从我的角度来看,设置看起来不错。

您已更改报告文件的默认名称。因此,我猜想您也需要将其告知 Sonar 分析仪。

单元测试的 jacoco 默认值是:${project.build.directory}/jacoco.exec这也是 Sonar 将尝试获取的名称。

您可以简单地设置一个属性来指示 Sonar 分析仪的位置:

<properties
<sonar.jacoco.reportPath>${project.build.directory}/coverage-reports/jacoco-ut.exec</sonar.jacoco.reportPath>
...

请验证您的 jacco 文件是否存在且非空 - 如果是这样,一旦位置配置正确, Sonar 迟早会拾取它。

关于java - maven jacoco 插件不生成覆盖率报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36199184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com