gpt4 book ai didi

java - 在单独的 maven 模块中获取单元和集成测试的 Sonar 正确覆盖

转载 作者:搜寻专家 更新时间:2023-10-31 20:17:24 25 4
gpt4 key购买 nike

我的项目设置很简单(所有源代码都可以在 github 获得):

parent
↳ 后端
↳ 客户
↳ 集成测试

在运行 maven 之后:

mci sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=12...9

我看到单元和集成测试对于 Sonar 是可见的,但 IT 的覆盖范围不是。 sonar coverage sonar coverage detailed

对于 Intelij IDEA jacoco-it.exec 看起来不错: idea coverage

我假设罪魁祸首在这里:

[INFO] Sensor JaCoCoSensor [java]
[INFO] No JaCoCo analysis of project coverage can be done since there is no class files.
[INFO] Sensor JaCoCoSensor [java] (done) | time=1ms

所以我做了小hack (简而言之:将所有源文件复制到 integration-test 模块):

    <properties>
<sonar.sources>${basedir}/target/copied</sonar.sources>
</properties>
[...]
<!-- hack to generate coverage reports -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven-resources-plugin.version}</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>generate-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${sonar.sources}</outputDirectory>
<resources>
<resource>
<directory>${basedir}/../backend/src/main/java</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${sonar.sources}</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

但现在我所有的类都重复了( Sonar 显示了 ../target/copied 目录中的类): sonar coverage duplicated

Sonar 版本:6.5
Jacoco maven 插件:0.7.5.201505241946(也尝试了最新的 0.7.9)

有什么想法我应该在这里做什么吗?

最佳答案

看来我已经找到了这个问题的答案:

  1. 报告未生成,因为 post-unit-test执行处于错误的阶段。而不是 <phase>test</phase>我现在有 <phase>verify</phase>

  2. post-integration-test 我的目标是错误的.更改来自 <goal>report-integration</goal><goal>report-aggregate</goal> :

    • This goal当测试与被测代码在不同的项目中时,允许我创建覆盖率报告。
  3. 添加的属性: <jacoco.itReportPath>${project.basedir}/../integrations-tests/target/jacoco-it.exec</jacoco.itReportPath><sonar.jacoco.reportPaths>${jacoco.itReportPath},${project.build.directory}/jacoco-it.exec,${project.build.directory}/jacoco.exec</sonar.jacoco.reportPaths>

github 上提供所有这些更改和更新项目

关于java - 在单独的 maven 模块中获取单元和集成测试的 Sonar 正确覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45762614/

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