maven 多模块项目的测试覆盖率分析 - jacoco 聚合分析

打印 上一主题 下一主题

主题 904|帖子 904|积分 2712

前言

对于大多数 maven 多模块化工程,可以使用 Jacoco 这款工具,关于 Jacoco 这款工具,ChatGPT 对它的描述是这样的:
JaCoCo(Java Code Coverage)是一个开源的测试覆盖率工具,它可以用于帮助开发人员衡量其软件测试的有效性。它支持多种语言,包括 Java 和 Kotlin 等,并且可以与多个构建工具和集成开发环境(IDE)一起使用。
JaCoCo 可以收集测试覆盖率数据,并生成可视化的测试覆盖率报告,帮助开发人员更好地理解其代码的测试覆盖率情况。它提供了多种测试覆盖率指标,例如行覆盖率、分支覆盖率、方法覆盖率、类覆盖率等,可以帮助开发人员了解其测试覆盖率情况的具体细节。
JaCoCo 还可以与多种构建工具集成,例如 Maven、Gradle 等。它可以通过 Maven 或 Gradle 的插件来收集测试覆盖率数据,并在构建过程中生成测试覆盖率报告
Jacoco 可以很好的支持对 Maven 多模块进行聚合分析测试覆盖率,可以从项目整体输出覆盖率报告非常方便。
下面展示一下具体的使用方法
一:创建根项目

先创建一个多模块的 Maven 项目,大致的结构如下:
  1. ├── parent-project
  2. ├── <packaging>pom</packaging>.xml
  3. ├── business-module1
  4. │   ├── <packaging>pom</packaging>.xml
  5. │   └── src
  6. │       ├── main
  7. │       └── test
  8. ├── business-module2
  9. │   ├── <packaging>pom</packaging>.xml
  10. │   └── src
  11. │       ├── main
  12. │       └── test
  13. └── test-module
  14.     ├── <packaging>pom</packaging>.xml
  15.     └── src
  16.         ├── main
  17.         └── test
复制代码
在一个空白的目录,一个的 Maven 的根项目:
  1. mvn archetype:generate \
  2. -DgroupId=org.example \
  3. -DartifactId=jacoco-multi-module-example \
  4. -DarchetypeArtifactId=maven-archetype-quickstart \
  5. -DinteractiveMode=false
复制代码
然后进入目录:
  1. cd jacoco-multi-module-example
复制代码
把根目录 <packaging>pom</packaging>.xml 的 packaging 属性改为 <packaging>pom</packaging>,从而将根目录设置为一个聚合模块,用来管理多个子模块的依赖关系
  1. <packaging>pom</packaging>
复制代码
二:创建子模块

根据上面的结构,在根目录下,分别创建:

  • business-module1
  • business-module2
  • test-module
在根目录的路径下,输入以下命令,创建 business-module1 模块:
  1. mvn archetype:generate \
  2. -DgroupId=org.example \
  3. -DartifactId=business-module1 \
  4. -DarchetypeArtifactId=maven-archetype-quickstart  \
  5. -DinteractiveMode=false
复制代码
创建 business-module2 模块:
  1. mvn archetype:generate \
  2. -DgroupId=org.example \
  3. -DartifactId=business-module2 \
  4. -DarchetypeArtifactId=maven-archetype-quickstart \
  5. -DinteractiveMode=false
复制代码
创建 test-module 单元测试模块:
  1. mvn archetype:generate \
  2. -DgroupId=org.example \
  3. -DartifactId=test-module \
  4. -DarchetypeArtifactId=maven-archetype-quickstart \
  5. -DinteractiveMode=false
复制代码
然后模拟实际的开发,分别在模块1,模块2中添加一些业务代码,
在 business-module1 中我添加一个简单的数学运算 IntegerSimpleCompute 类:
  1. // business-module1\src\main\java\org\example\IntegerSimpleCompute.java
  2. package org.example;
  3. public class IntegerSimpleCompute {
  4.     public int add(int i, int j) {
  5.         return i + j;
  6.     }
  7.     public int subtract(int i, int j) {
  8.         return i - j;
  9.     }
  10.     public int multiply(int i, int j) {
  11.         return i * j;
  12.     }
  13.     public int divide(int i, int j) {
  14.         return i / j;
  15.     }
  16. }
复制代码
在 business-module2 中我添加一个简单的逻辑运算 IntegerLogicCompute 类:
  1. // business-module2\src\main\java\org\example\IntegerLogicCompute.java
  2. package org.example;
  3. public class IntegerLogicCompute {
  4.     public int increment(Integer i) {
  5.         return i + 1;
  6.     }
  7.     public int decrement(Integer i) {
  8.         return i- 1;
  9.     }
  10.     // 存在条件分支的语句,需要满足所有条件分支判断才能达到 100% 的覆盖率
  11.     public boolean equals(Integer i, Integer j) {
  12.         if (i < 127 && j < 127) {
  13.             return i == j;
  14.         }
  15.         return i.equals(j);
  16.     }
  17. }
复制代码
三:创建测试模块

我们将 test-module 作为测试模块,在该模块的 <packaging>pom</packaging>.xml 文件中,我们引入上面的测试模块,方便对他们进行集成测试
  1. <dependencies>
  2.     <dependency>
  3.         <groupId>org.example</groupId>
  4.         <artifactId>business-module1</artifactId>
  5.         <version>1.0-SNAPSHOT</version>
  6.     </dependency>
  7.     <dependency>
  8.         <groupId>org.example</groupId>
  9.         <artifactId>business-module2</artifactId>
  10.         <version>1.0-SNAPSHOT</version>
  11.     </dependency>
  12.     <dependency>
  13.         <groupId>junit</groupId>
  14.         <artifactId>junit</artifactId>
  15.         <version>4.12</version>
  16.         <scope>test</scope>
  17.     </dependency>
  18. </dependencies>
复制代码
然后在 src/test/java 目录下创建测试类:
  1. // test-module\src\test\java\org\example\IntegrationTest.java
  2. package org.example;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. import static org.junit.Assert.assertEquals;
  6. public class IntegrationTest {
  7.     private IntegerSimpleCompute simpleCompute;
  8.     private IntegerLogicCompute logicCompute;
  9.     @Before
  10.     public void init() {
  11.         simpleCompute = new IntegerSimpleCompute();
  12.         logicCompute = new IntegerLogicCompute();
  13.     }
  14.     @Test
  15.     public void simpleComputeTest() throws Throwable {
  16.         assertEquals(7, simpleCompute.add(3, 4));
  17.         assertEquals(4, simpleCompute.subtract(7, 3));
  18.         assertEquals(12, simpleCompute.multiply(3, 4));
  19.         assertEquals(3, simpleCompute.divide(12, 4));
  20.     }
  21.     @Test
  22.     public void logicComputeTest() throws Throwable {
  23.         assertEquals(8, logicCompute.increment(7));
  24.         assertEquals(6, logicCompute.decrement(7));
  25.         assertEquals(true, logicCompute.equals(125, 125));
  26.         assertEquals(false, logicCompute.equals(123, 125));
  27.         assertEquals(false, logicCompute.equals(123, 130));
  28.         assertEquals(false, logicCompute.equals(133, 125));
  29.         assertEquals(true, logicCompute.equals(140, 140));
  30.         assertEquals(false, logicCompute.equals(140, 141));
  31.     }
  32. }
复制代码
到可以,你可以通过:
  1. mvn test
复制代码
执行单元测试,maven 的 maven-surefire-plugin 插件也会简单的输出如下测试报告:
  1. Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
复制代码
四:生成覆盖率报告

首先在根目录的 <packaging>pom</packaging>.xml 引入 jacoco 插件并且启动代理:
  1. <build>
  2.     <plugins>
  3.         
  4.         <plugin>
  5.             <groupId>org.apache.maven.plugins</groupId>
  6.             <artifactId>maven-compiler-plugin</artifactId>
  7.             <version>3.8.1</version>
  8.             <configuration>
  9.                 <source>11</source>
  10.                 <target>11</target>
  11.             </configuration>
  12.         </plugin>
  13.         
  14.         <plugin>
  15.             <groupId>org.jacoco</groupId>
  16.             <artifactId>jacoco-maven-plugin</artifactId>
  17.             <version>0.8.8</version>
  18.             <executions>
  19.                
  20.                 <execution>
  21.                     <id>default-prepare-agent</id>
  22.                     <goals>
  23.                         <goal>prepare-agent</goal>
  24.                     </goals>
  25.                 </execution>
  26.                
  27.                 <execution>
  28.                     <id>report</id>
  29.                     <phase>verify</phase>
  30.                     <goals>
  31.                         <goal>report</goal>
  32.                     </goals>
  33.                 </execution>
  34.             </executions>
  35.         </plugin>
  36.     </plugins>
  37. </build>
复制代码
然后在 test-module 模块中引入 jacoco 插件,声明一个聚合分析任务:
  1. <build>
  2.     <plugins>
  3.         <plugin>
  4.             <groupId>org.jacoco</groupId>
  5.             <artifactId>jacoco-maven-plugin</artifactId>
  6.             <version>0.8.8</version>
  7.             <executions>
  8.                
  9.                 <execution>
  10.                     <id>report-aggregate</id>
  11.                     <phase>verify</phase>
  12.                     <goals>
  13.                         <goal>report-aggregate</goal>
  14.                     </goals>
  15.                 </execution>
  16.             </executions>
  17.         </plugin>
  18.     </plugins>
  19. </build>
复制代码
最后在根目录执行指令,运行所有测试:
  1. $ mvn clean verify
复制代码
构建成功后可以在 test-module 模块下的 target/site/jacoco-aggregate/index.html 查看覆盖率报告:

点击对应模块可以看到包内部所有类,方法还有每一行的测试覆盖率情况,这里具体不再展开,自己可以尝试以下
示例代码:jacoco-module-sample
参考资料:

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

络腮胡菲菲

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表