java - 当单元测试覆盖率低于多模块项目中预先配置的限制时,maven 构建失败

标签 java maven code-coverage jacoco jacoco-maven-plugin

有人可以告诉我,当聚合单元测试覆盖率低于 Maven 多模块项目中预先配置的限制时,我如何才能使构建失败?

例如,假设 module1 的代码覆盖率是 70%,module2 的代码覆盖率是 100%,如果指定了 90% 的覆盖率,则构建应该失败平均覆盖率为 85%。

我正在使用 jacoco 生成代码覆盖率,以生成包含覆盖率数据的聚合 .exec 文件。但正如所讨论的here jacoco:check 仅适用于模块级别。

看来仅靠 jacoco 无法完成我正在寻找的工作。有人可以让我知道我可以在聚合的 jacoco.exec 文件上使用哪些其他工具来完成这项工作吗?

尝试过的方法

按照我的@Karol的建议

我的项目结构如下

main
- module1
- module2

main.pom 中,我添加了以下部分

<plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.5.201505241946</version>
              <executions>
                <execution>
                  <id>pre-unit-test</id>
                  <goals>
                      <goal>prepare-agent</goal>
                  </goals>
                  <configuration>
                    <destFile>${jacoco.ut.execution.data.file}</destFile>
                  </configuration>
                </execution>
                <execution>
                  <id>merge-execs</id>
                  <phase>pre-site</phase>
                  <inherited>false</inherited>
                  <goals>
                      <goal>merge</goal>
                  </goals>
                  <configuration>
                   <fileSets>
                     <fileSet>
                       <directory>${basedir}</directory>
                       <includes>
                         <include>**/target/*.exec</include>
                       </includes>
                     </fileSet>
                   </fileSets>
                    <destFile>${jacoco.ut.merged.exec}</destFile>
                  </configuration>
                </execution>
                  <execution>
                      <id>jacoco-check</id>
                      <phase>verify</phase>
                      <goals>
                          <goal>check</goal>
                      </goals>
                      <configuration>
                          <rules>
                              <rule>
                                  <element>BUNDLE</element>
                                  <limits>
                                      <limit>
                                          <counter>LINE</counter>
                                          <value>COVEREDRATIO</value>
                                          <minimum>0.80</minimum>
                                      </limit>
                                  </limits>
                              </rule>
                          </rules>
                          <dataFile>${jacoco.ut.merged.exec}</dataFile>
                      </configuration>
                  </execution>
             </executions>
         </plugin>

然后在执行 mvn pre-site 时,我可以看到合并的 exec 文件正在创建,如下所示

[INFO] Loading execution data file /var/code/github/vnera/main/module1/target/jacoco.exec
[INFO] Loading execution data file /var/code/github/vnera/main/module2/target/jacoco.exec
[INFO] Writing merged execution data to /var/code/github/vnera/main/target/jacoco_vrni_main.exec

但是当我运行 mvn jacoco:check@jacoco-check 时,我看到检查被跳过,并显示以下消息

[INFO] --- jacoco-maven-plugin:0.7.5.201505241946:check (jacoco-check) @ main ---
[INFO] Skipping JaCoCo execution due to missing classes directory:/var/code/github/vnera/main/target/classes

最佳答案

使用 jacoco:merge 定义构建中的步骤目标:

Mojo for merging a set of execution data files (*.exec) into a single file

然后申请 jacoco:check 通过使用 <dataFile> 指向此合并的覆盖文件目标属性。

<小时/>

或者您可以设置 SonarQubecorrect JaCoCo multi module coverage并配置Quality Gates确保最小的代码覆盖率:

A quality gate is the best way to enforce a quality policy in your organization. It's there to answer ONE question: can I deliver my project to production today or not?

In order to answer this question, you define a set of Boolean conditions based on measure thresholds against which projects are measured. For example:

  • No new blocker issues
  • Code coverage on new code greater than 80%
  • Etc.

关于java - 当单元测试覆盖率低于多模块项目中预先配置的限制时,maven 构建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57361267/

相关文章:

Javaagent 路径在 maven/linux/tomcat 上没有得到很好的解释

code-coverage - Allure 和代码覆盖率报告

c# - 从测试方法传递空对象

java - 调用 void 方法并传递参数 RxJava

java - 如何比较列表和字符串数组(Java)?

java - 将任何字符串转换为 UTF-8 格式

java - 泰坦,RexterClient :Message received response timeoutConnection (8000 s) at com.tinkerpop.rexster.client.RexsterClient.execute(RexsterClient.java:185)

java - Maven 插件的部署问题

python - 确保单元测试中的代码覆盖率?

java - 如何横切带注释的方法和构造函数?