测试覆盖率统计
JaCoCo 插件生成测试覆盖率报告,监控测试质量。
JaCoCo 配置
基本配置
XML
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
生成报告
执行命令
Bash
mvn test
报告位置
XML
target/site/jacoco/index.html
覆盖率阈值
配置阈值检查
text
<execution>
<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> <!-- 行覆盖率 ≥ 80% -->
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
覆盖率指标
| 指标 | 说明 |
|---|---|
| LINE | 行覆盖率 |
| BRANCH | 分支覆盖率 |
| METHOD | 方法覆盖率 |
| CLASS | 类覆盖率 |
要点总结
- JaCoCo 生成测试覆盖率报告
- prepare-agent 准备代理
- report 生成报告
- check 验证覆盖率阈值
- target/site/jacoco/index.html 报告位置
📝 发现内容有误?点击此处直接编辑