使用JaCoCo统计单元测试代码覆盖率
1 JaCoCo介绍
JaCoCo是EclEmma团队基于多年覆盖率库使用经验总结而研发的一个开源的Java代码覆盖率库。
2 JaCoCo覆盖率计数器
3 Gradle单模块接入
在工程的buid.gradle文件中添加如下内容:
//Applying the JaCoCo plugin plugins { id 'jacoco' } // Configuring JaCoCo plugin settings jacoco { toolVersion = "0.8.4" //reportsDir = file("$buildDir/customJacocoReportDir") } //Configuring test task jacocoTestReport { reports { xml.enabled false csv.enabled false html.enabled true //html.destination file("${buildDir}/jacocoHtml") } } check.dependsOn jacocoTestReport
编译完成后会在 ${buildDir}/build/reports/jacoco下生成报告。
如果在Spring Boot的框架下运行,需要在build.gradle中加下面一段,否则执行jacocoTestReport task会被skipped。
tasks.withType(Test) { scanForTestClasses = false include "**/*Test.class" }
4 Gradle多模块接入
在父子工程的buid.gradle文件中添加如下内容,注意父和子都要添加:
//Applying the JaCoCo plugin apply plugin:'jacoco' // Configuring JaCoCo plugin settings jacoco { toolVersion = "0.8.4" //reportsDir = file("$buildDir/customJacocoReportDir") } //Configuring test task jacocoTestReport { reports { xml.enabled false csv.enabled false html.enabled true //html.destination file("${buildDir}/jacocoHtml") } } check.dependsOn jacocoTestReport
编译完成后会在对应的子工程的 ${buildDir}/build/reports/jacoco下生成报告。
5 Maven单模块接入
在工程的pom.xml文件中添加如下内容:
<build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.4</version> <executions> <execution> <id>jacoco-initialize</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>jacoco-site</id> <phase>package</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
执行Run As Maven build:
clean install
在项目target/site/jacoco目录下找到index.html文件,即可查看报告。
6 Maven多模块接入
Maven多模块是指存在父子关联的项目,这类项目中存在多个pom.xml文件,父项目pom.xml文件中包括多个<module>标签,指明它的子模块有哪些,子项目pom.xml中能继承父项目配置的依赖,通过<parent>标签能知道它的父模块是谁。
6.1 父pom中增加依赖
在多模块项目中找到父pom.xml文件,添加如下内容:
<build> <pluginManagement> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.4</version> </plugin> </plugins> </pluginManagement> </build>
6.2 新建子覆盖率模块
该模块添加所有要进行单元测试的子工程的依赖,例如,新增一个jacoco-coverage的子模块,在pom.xml文件里添加如下内容:
增加<dependency>指定统计哪些module
<dependencies> <dependency> <groupId>org.sonatype.mavenbook.multi</groupId> <artifactId>simple-weather</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.sonatype.mavenbook.multi</groupId> <artifactId>simple-webapp/artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> </dependencies>
添加jacoco-maven-plugin
<build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.4</version> <executions> <execution> <id>jacoco-initialize</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <phase>verify</phase> <goals> <goal>report-aggregate</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
6.3 父pom中增加该模块
<modules>
<module>simple-weather</module>
<module>simple-webapp</module>
<module>jacoco-coverage</module>
</modules>
执行Run As Maven build:
clean install
在子项目jacoco-coverage生成的target/site/jacoco-aggregate目录下找到index.html文件并打开即可查看报告。
以下给出一个官网提供的报告: