简体   繁体   English

JaCoCo 插件提供了不同的覆盖范围

[英]JaCoCo plugin gives different coverage

I'm using the JaCoCo Maven plugin and agent to measure and retrieve the code coverage data of an application which is tested nightly.我正在使用 JaCoCo Maven 插件和代理来测量和检索每晚测试的应用程序的代码覆盖率数据。 This is the schema of the architecture:这是架构的架构:

测试架构

My Maven project is configured with the JMeter Maven plugin to execute some API tests during the Maven verify phase.我的 Maven 项目配置了JMeter Maven 插件,以在 Maven verify阶段执行一些 API 测试。 The Maven command executed by the Jenkins server is the following Jenkins服务器执行的Maven命令如下

mvn verify org.jacoco:jacoco-maven-plugin:0.7.8:dump sonar:sonar -Djacoco.address=TEST_SERVER -Djacoco.destFile=/proj/coverage-reports/jacoco-it.exec -Dsonar.projectKey=sonar_test -Dsonar.projectName=sonar_test -Dsonar.branch=sonar_test -Dsonar.jacoco.itReportPath=/proj/coverage-reports/jacoco-it.exec -Dsonar.java.coveragePlugin=jacoco -Dsonar.language=java

As you can see first the tests are executed through the verify phase, then the jacoco:dump goal retrieves the coverage data from the test server (I configured the server to run the JaCoCo agent) and at last the data is uploaded to my Sonar server.正如您所看到的,首先测试是通过verify阶段执行的,然后jacoco:dump目标从测试服务器(我将服务器配置为运行 JaCoCo 代理)检索覆盖数据,最后将数据上传到我的声纳服务器.

The "strange" behaviour I'm having is that if I run this command on my computer and then on Jenkins (configuring the Jenkins project accordingly) in the SonarQube page I get different coverage results.我遇到的“奇怪”行为是,如果我在我的计算机上运行此命令,然后在 SonarQube 页面中的 Jenkins(相应地配置 Jenkins 项目)上运行,我会得到不同的覆盖结果。 Moreover, if I configure the Jenkins project and then I simply COPY it creating a new (but equivalent) Jenkins project, the results are different.此外,如果我配置 Jenkins 项目,然后我简单地复制它,创建一个新的(但等效的)Jenkins 项目,结果是不同的。

本地测试的覆盖结果

Jenkins 的覆盖结果

I tried different configurations and cases, but I cannot understand what the problem can be.我尝试了不同的配置和案例,但我无法理解问题是什么。 Am I not considering some JaCoCo constraints (eg someting related to the Jenkins project name)?我是不是没有考虑一些 JaCoCo 限制(例如与 Jenkins 项目名称相关的一些限制)?

As said in the question comments, the artifact deployed on the test server and the one compiled during the verify phase on which the report is generated must be exactly the same, so it is not enough that the code is the same.正如问题评论中所说,部署在测试服务器上的工件和生成报告的verify阶段编译的工件必须完全相同,因此代码相同是不够的。

To solve my problem I had to implement this workflow with Jenkins:为了解决我的问题,我必须用 Jenkins 实现这个工作流程:

  1. Do a mvn package on the project在项目上做一个mvn package
  2. Deploy the generated WAR on the remote server using Ansible (we already use Ansible for nightly deploys and other tasks on remote machines)使用 Ansible 在远程服务器上部署生成的 WAR(我们已经使用 Ansible 在远程机器上进行夜间部署和其他任务)
  3. Run the remote tests without recompiling the wars.无需重新编译战争即可运行远程测试。 To do this I had to add the Maven flag -Dmaven.compiler.useIncrementalCompilation=false (thanks to this and this for the hints) in order to not re-compile the artifacts during the verify phase为此,我必须添加 Maven 标志-Dmaven.compiler.useIncrementalCompilation=false (感谢thisthis的提示),以便在verify阶段不重新编译工件
  4. Retrieve ( dump ) the JaCoCo coverage data检索( dump )JaCoCo 覆盖数据

So the Maven command described in the question has been split in two commands: the one which creates the package and the one which performs the tests and retrieves the JaCoCo data without recompiling the artifacts.因此,问题中描述的 Maven 命令已拆分为两个命令:一个创建包,另一个执行测试并检索 JaCoCo 数据,而无需重新编译工件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 maven jacoco 插件不生成覆盖率报告 - maven jacoco plugin does not generate coverage report JUnit5 的 Jacoco 插件失败,没有覆盖 - Jacoco plugin fails for JUnit5 with no coverage Gradle Jacoco 插件报告零覆盖 - Gradle Jacoco Plugin Reporting Zero Coverage Clover和Jacoco给出不同的代码覆盖率结果 - Clover and Jacoco give different code coverage results 不同的 jacoco 覆盖导致 github 动作环境 - Different jacoco coverage results in a github action environment 使用 orElseThrow 为 Optional 编写 Mockito 单元测试:Jacoco 的代码覆盖率为 0% - Writing Mockito Unit test for Optional with orElseThrow : Jacoco gives 0% code coverage 使用 JaCoCo 插件配置分析 Maven 插件后,SonarQube 服务器上缺少 JaCoCo 代码覆盖率报告 - Missing JaCoCo code coverage report on SonarQube server after analyzing Maven plugin with JaCoCo plugin configuration 使用 JaCoCo 和 spring-boot-maven-plugin 生成代码覆盖率 - Generate Code Coverage with JaCoCo and spring-boot-maven-plugin 如何使用jacoco获得不同回购中测试的测试覆盖率? - How to get test coverage of tests in different repo using jacoco? 当代码和单元测试在不同模块中时,Quarkus Jacoco 覆盖率不起作用 - Quarkus Jacoco coverage not working when code and unit tests are in different modules
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM