一、工具选型
简介:jacoco(Java code coverage )是目前主流的开源代码覆盖率统计工具(更新最热,github引用最多)。其实现原理(on-the-fly模式)为通过java.lang.instrument包提供的接口,编写jvm代理,在jvm加载字节码时动态修改内容,增加探针指令。
二、代码覆盖率统计原理
处理流程:
动态加入代码探针效果:
原代码:
经jacoco jvm代理处理后的代码:
(实际为字节码,为便于展示,替换为等效的源码)
三、配置
单模块应用maven配置
在项目pom增加maven插件
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>prepare-unit-tests</id>
<goals>
<!--default bound to phase:initialize-->
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report-when-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin> |
此配置的作用是在maven生命周期的initialize阶段执行prepare-agent,修改字节码加入探针;在生命周期的test阶段执行report,生成报告。
多模块应用maven配置
在父pom配置jacoco插件
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>prepare-unit-tests</id>
<!--default bound to phase:initialize-->
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin> |
这一步的作用是在应用启动的时候附加上-javaagent参数,对字节码做修改,加入探针
新增test-reporting子模块
子模块只包含一个pom文件,包含两块配置:
依赖所有其他模块
<dependencies>
<dependency>
<groupId>com.zhengwenbin</groupId>
<artifactId>module1</artifactId>
<version>${my.project.version}</version>
</dependency>
<dependency>
<groupId>com.zhengwenbin</groupId>
<artifactId>module2</artifactId>
<version>${my.project.version}</version>
</dependency>
</dependencies> |
此配置的作用是,将各个模块的测试报告汇总到一处。(jacoco官方提供汇总多模块应用测试报告的方法)
配置jacoco插件
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>report-aggregate</id>
<phase>test</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
<execution>
<id>merge-results</id>
<phase>test</phase>
<goals>
<goal>merge</goal>
</goals>
<configuration>
<fileSets>
<fileSet>
<directory>${code.coverage.project.folder}</directory>
<includes>
<include>**/target/jacoco.exec</include>
</includes>
</fileSet>
</fileSets>
<destFile>${code.coverage.overall.data.folder}/aggregate.exec</destFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
|
此配置的作用是,将各个模块的测试报告聚合,并将生成报告动作绑定到test阶段
多模块应用配置示例:
四、使用
在项目pom.xml目录下执行命令:
mvn clean test
五、结果查看
覆盖率报告html
查看覆盖率
jacoco插件report/report-aggregate命令会在各个模块的target目录生成site文件夹,进入site/jacoco文件夹,使用浏览器打开index.html。(对于多模块应用,site目录只会显示该模块依赖的模块代码覆盖率,因此需要进入test-reporting模块查看所有报告)
可以看到指令、分支、行、方法、类级别的代码覆盖率报告。(默认只显示指令覆盖率百分比,没有找到jacoco显示其他参数百分比的配置项)
查看未覆盖的代码行
在element列不断点击链接,可看到不同层级的代码覆盖率,最终可以看到代码行的指令覆盖情况:
- 绿色表示完全覆盖
- 红色表示未覆盖
- 黄色表示部分覆盖
多模块聚合报告.exec文件
对于多模块应用,在html报告中只能看到分模块的覆盖率。若要查看根据类路径聚合的测试报告,需要使用IDE或者jar包工具,打开jacoco.exec文件。
- 文件地址:多模块项目根目录/target/jacoco.exex
- 打开方式:idea →Run → Show Coverage Data → 点击+号,选择jacoco.exec文件;最终展示如下:
点击按钮,可以生成html版本的报告
六、开发者工具
idea原生插件coverage
上述代码覆盖率报告适用于生成最终验收报告,实际开发过程中,可以使用ide工具来为每一次运行生成覆盖率报告,并将行覆盖情况显示在ide编辑器中。使用ide生成测试覆盖率报告,无需任何pom配置。
官方文档:https://www.jetbrains.com/help/idea/code-coverage.html
新增junit run configuration
以覆盖率工具方式运行测试用例
或者选中测试用例,点击小盾牌按钮
运行后,将弹出窗口,要求选择如何显示覆盖率数据,选择替换;coverage窗口将自动弹出。
查看代码覆盖情况: