使用MAVEN+Jacoco统计单元测试的代码覆盖率

1 篇文章 0 订阅

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插件配置:

pom.xml
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进行具体查看

 


 


 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值