Springboot 多模块项目集成Jacoco统计单元测试覆盖率

最外层POM配置

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                    <forkMode>once</forkMode>
                    <reuseForks>true</reuseForks>
                    <useSystemClassLoader>false</useSystemClassLoader>
                    <argLine>@{argLine}</argLine>
                    <includes>
                        <include>**/*Test.java</include>
                        <include>**/*Test*.java</include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.6</version>
                <executions>
                    <execution>
                        <id>my-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <propertyName>surefireArgLine</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>my-report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report-aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

本地统计代码单元测试覆盖率

mvn test jacoco:prepare-agent jacoco:report jacoco:report-aggregate 

远端SonarQube执行

jenkis服务器需要配置Jacoco

  mvn test jacoco:prepare-agent jacoco:report jacoco:report-aggregate sonar:sonar -Dsonar.projectKey=xxxxx -Dsonar.projectName=xxx

注意事项

Q1 target目录下未编译test目录下的代码

常见报错:

Not compiling test sources

Skipping JaCoCo execution due to missing execution data file.

修改插件配置

 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${version.compiler.plugin}</version>
                <configuration>
                     <!--.....-->
                    <skip>false</skip><!--注意此处-->
                    <!--.....-->
                </configuration>
 </plugin>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Jacoco 是一个代码覆盖率工具,可以帮助我们衡量测试用例对代码的覆盖率,从而更好地了解代码的质量和测试的充分性。要在 Spring Boot 多模块项目中使用 Jacoco 进行单元测试,可以采取以下步骤: 1. 在每个子模块的 `pom.xml` 文件中添加 Jacoco 插件的配置,如下所示: ``` <build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.7</version> <executions> <execution> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>test</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 这段配置用于在每个子模块的测试前启动 Jacoco 插件,生成代码覆盖率报告。 2. 在父模块的 `pom.xml` 文件中添加 Jacoco 插件的配置,如下所示: ``` <build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.7</version> <executions> <execution> <id>report-aggregate</id> <phase>verify</phase> <goals> <goal>report-aggregate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 这段配置用于在父模块的测试完成后,收集每个子模块的代码覆盖率数据,并生成聚合报告。 3. 进入每个子模块的目录,执行 `mvn clean test` 命令,生成代码覆盖率报告。 4. 在父模块的目录下执行 `mvn clean verify` 命令,生成聚合报告。 5. 打开聚合报告的路径,查看每个子模块的代码覆盖率数据和总体覆盖率数据。 注意:在使用 Jacoco 生成代码覆盖率报告时,需要保证测试用例覆盖到了代码的所有分支和情况,才能准确地衡量代码的覆盖率

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值