JaCoCo是EclEmma团队基于多年覆盖率库使用经验总结而研发的一个开源的Java代码覆盖率库。
html
在工程的buid.gradle文件中添加以下内容:web
//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下生成报告。app
若是在Spring Boot的框架下运行,须要在build.gradle中加下面一段,不然执行jacocoTestReport task会被skipped。框架
tasks.withType(Test) { scanForTestClasses = false include "**/*Test.class" }
在父子工程的buid.gradle文件中添加以下内容,注意父和子都要添加:webapp
//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下生成报告。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文件,便可查看报告。gradle
Maven多模块是指存在父子关联的项目,这类项目中存在多个pom.xml文件,父项目pom.xml文件中包括多个<module>标签,指明它的子模块有哪些,子项目pom.xml中能继承父项目配置的依赖,经过<parent>标签能知道它的父模块是谁。ui
在多模块项目中找到父pom.xml文件,添加以下内容:
<build> <pluginManagement> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.4</version> </plugin> </plugins> </pluginManagement> </build>
该模块添加全部要进行单元测试的子工程的依赖,例如,新增一个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>
<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文件并打开便可查看报告。
如下给出一个官网提供的报告: