1、简介
Jacoco是一个开源的覆盖率工具。Jacoco可以嵌入到Ant 、Maven中,并提供了EclEmma Eclipse插件,也可以使用JavaAgent技术监控Java程序。很多第三方的工具提供了对Jacoco的集成,如sonar、Jenkins等。之所以在此引入Jacoco是因为在使用Cobertura的时候,不能完全的适配Jdk 8 Lambda表达式,而Jacoco可以适配。
官网地址:http://www.eclemma.org/jacoco/
2、pom文件中配置Jacoco插件
在pom文件中添加Jacoco插件配置:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
< plugin >
< groupId >org.apache.maven.plugins</ groupId >
< artifactId >maven-surefire-plugin</ artifactId >
< configuration >
< skip >false</ skip >
< testFailureIgnore >true</ testFailureIgnore >
< argLine >${surefireArgLine}</ argLine >
</ configuration >
</ plugin >
< plugin >
< groupId >org.jacoco</ groupId >
< artifactId >jacoco-maven-plugin</ artifactId >
< version >0.7.8</ version >
< configuration >
< destFile >${project.build.directory}/coverage-reports/jacoco-ut.exec</ destFile >
< dataFile >${project.build.directory}/coverage-reports/jacoco-ut.exec</ dataFile >
< skip >${skipTests}</ skip >
< output >file</ output >
< append >true</ append >
</ configuration >
< executions >
< execution >
< id >pre-unit-test</ id >
< goals >
< goal >prepare-agent</ goal >
</ goals >
< configuration >
< propertyName >surefireArgLine</ propertyName >
</ configuration >
</ execution >
< execution >
< id >post-unit-test</ id >
< phase >test</ phase >
< goals >
< goal >report</ goal >
</ goals >
< configuration >
< outputDirectory >${project.reporting.outputDirectory}/jacoco-ut</ outputDirectory >
</ configuration >
</ execution >
</ executions >
</ plugin >
|
3、运行Jacoco
3.1、命令行运行
在命令行中进入项目,运行命令“mvn install”。
3.2、在Eclipse中运行
配置相应的Run->install,并点击运行。
3.4、确定Jacoco运行成功
4、查看报告
进入项目/target/site/,其下的整个Jacoco-ut都是报告相关内容,可以点击index.html进行具体查看

所有评论(0)