前言

jacoco是一个开源的覆盖率工具,它针对的开发语言是java,其使用方法很灵活,可以插桩到Ant、Maven中,可以使用其JavaAgent技术监控Java程序等。

那么本次主要使用对java代码进行覆盖率统计,比如A测试攻城狮在功能测试时/接口测试时,实际覆盖测试了多少java代码,可以作为一个评判。

jacoco官网地址:http://www.eclemma.org(有墙)

一、jacoco基本介绍

1、jacoco是根据控制流Type来采用不同的探针插桩策略的。(植入探针)

2、探针原理是字节码指令集插入到java方法中,程序执行后可以被记录,它不会改变原有代码的行为。

3、jacoco支持on-the-fly和offline的两种插桩模式。本次主要使用在java -jar xx.jar启动的时候加入参数(on-the-fly)

行覆盖率:度量被测程序的每行代码是否被执行,判断标准行中是否至少有一个指令被执行。

类覆盖率:度量计算class类文件是否被执行。

分支覆盖率:度量if和switch语句的分支覆盖情况,计算一个方法里面的总分支数,确定执行和不执行的 分支数量。

方法覆盖率:度量被测程序的方法执行情况,是否执行取决于方法中是否有至少一个指令被执行。

指令覆盖:计数单元是单个java二进制代码指令,指令覆盖率提供了代码是否被执行的信息,度量完全 独立源码格式。

圈复杂度:在(线性)组合中,计算在一个方法里面所有可能路径的最小数目,缺失的复杂度同样表示测 试案例没有完全覆盖到这个模块。

二、必备环境

本篇基于Windows下使用:

1、ant 环境

所有版本:https://downloads.apache.org/ant/binaries/

本篇直接点击此链接下载:https://downloads.apache.org/ant/binaries/apache-ant-1.9.15-bin.zip   下载后直接解压:

注意Windows后缀为.zip,如:apache-ant-1.9.15-bin.zip

设置环境变量:

①将bin、lib目录添加至环境变量中的系统变量path即可,如下图我的解压路径:

D:\apache-ant-1.9.15\bin
D:\apache-ant-1.9.15\lib

②cmd验证环境:

cmd输入:ant -v

如下图即可显示ant的版本为:1.9.15

2、jacoco

全部版本下载地址:https://repo1.maven.org/maven2/org/jacoco/jacoco/

可在我网盘中下载链接:https://pan.baidu.com/s/19evt3K1eo1y7GeeyggD5Hg 提取码:gsxl

强烈推荐clone git:https://github.com/git-gsxl/jacoco-demo.git

随你自己解压到哪个目录,

3、java.jar 与 build.xml 文件 

文件在网盘中下载链接:https://pan.baidu.com/s/19evt3K1eo1y7GeeyggD5Hg 提取码:gsxl

强烈推荐clone git:https://github.com/git-gsxl/jacoco-demo.git

4、配置build.xml文件

①配置 jacocoant.jar 文件路径;

②配置源码路径

③配置server_ip

④其它自行根据所需配置

三、启动 jar 服务

1、在 jar 包cmd下输入启动命令:java -javaagent:F:/jacoco-0.8.6/lib/jacocoagent.jar=includes=*,output=tcpserver,port=9999,address=127.0.0.1,append=true -jar demo-V1024.jar

通过javaagentJavaAgent会监听服务器端设置的TCP port,然后把收集到的信息(jacoco.exec)写入TCP connection,所以服务不需要关闭。

C:\Users\Administrator\Desktop\jacoco-demo>java -javaagent:F:/jacoco-0.8.6/lib/jacocoagent.jar=includes=*,output=tcpserver,port=9999,address=127.0.0.1,append=true -jar demo-V1024.jar

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.4.RELEASE) 2020-10-24 18:08:50.125 INFO 13264 --- [ main] c.s.j.jacocodemo.JacocodemoApplication : Starting JacocodemoApplication vV1024 on WIN-Q3LSDAOKUJ1 with PID 13264 (C:\Users\Administrator\Desktop\jacoco-demo\demo-V1024.jar started by Administrator in C:\Users\Administrator\Desktop\jacoco-demo)
2020-10-24 18:08:50.133 INFO 13264 --- [ main] c.s.j.jacocodemo.JacocodemoApplication : No active profile set, falling back to default profiles: default
2020-10-24 18:08:53.933 INFO 13264 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-10-24 18:08:53.989 INFO 13264 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-10-24 18:08:53.989 INFO 13264 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.38]
2020-10-24 18:08:54.195 INFO 13264 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-10-24 18:08:54.222 INFO 13264 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3949 ms
2020-10-24 18:08:54.748 INFO 13264 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-10-24 18:08:55.272 INFO 13264 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-10-24 18:08:55.301 INFO 13264 --- [ main] c.s.j.jacocodemo.JacocodemoApplication : Started JacocodemoApplication in 6.268 seconds (JVM running for 7.3)

2、运行 ant 命令

在此之前我们在浏览器上访问我们的服务,ip+prot (既我已经访问了hello的方法,业务测试覆盖了hello方法)

①ant dump

C:\Users\Administrator\Desktop\jacoco-demo>ant dump
Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre1.8.0_151\lib\tools.jar
Buildfile: C:\Users\Administrator\Desktop\jacoco-demo\build.xml dump:
[jacoco:dump] Connecting to /127.0.0.1:9999
[jacoco:dump] Dumping execution data to C:\Users\Administrator\Desktop\jacoco-report\jacoco-integation.exec BUILD SUCCESSFUL
Total time: 0 seconds C:\Users\Administrator\Desktop\jacoco-demo>

②ant report

C:\Users\Administrator\Desktop\jacoco-demo>ant report
Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre1.8.0_151\lib\tools.jar
Buildfile: C:\Users\Administrator\Desktop\jacoco-demo\build.xml report:
[mkdir] Created dir: C:\Users\Administrator\Desktop\jacoco-demo\jacoco-report
[jacoco:report] Loading execution data file G:\5-ppl-study-java\jacoco-demo\target\coverage\jacoco-integation.exec
[jacoco:report] Writing bundle 'Check Order related' with 2 classes BUILD SUCCESSFUL
Total time: 0 seconds

四、查看覆盖率报告

1、在jacoco-report目录下打开,index.html即可

2、点击进入 controller 查看详情

可以发现与我们上述所示的一致,只覆盖hello方法。剩下看你们了,去试试运行其它方法,再执行ant,再查看报告。

欢迎来大家QQ交流群一起学习:482713805

最新文章

  1. 由Photoshop高反差保留算法原理联想到的一些图像增强算法。
  2. 浅析 Android 的窗口
  3. 使用Java判断字符串中的中文字符数量
  4. java 选择排序
  5. linux下安装或升级GCC4.8,以支持C++11标准
  6. SQLAlchemy 做migration的时候 ValueError: too many values to unpack
  7. 转:Linux集群-----HA浅谈
  8. YUI Reset CSS (学习摘抄)
  9. SVN提交数据失败问题(提示 svn:MKACTIVITY ... 403 Forbidden )
  10. c语言背后的运行机制
  11. 纯手工打造dropdownlist控件
  12. lol.py
  13. SAP MM 启用批次管理的物料MB21创建预留单据时批次号可以为空!
  14. JS(JavaScript)的进一步了解5(更新中···)
  15. HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径)
  16. SVG 学习<六> SVG的transform
  17. ZH奶酪:基于ionic.io平台的ionic消息推送功能实现
  18. (转)Ubuntu12.04上NFS Server安装使用过程
  19. DevExpress WinForms Controls 学习系列1
  20. Mybatis基础及入门案例

热门文章

  1. 4.Strom-可靠性保证
  2. Boolean.valueOf(String)
  3. @Autowried入门和源码分析
  4. JVM学习(五)对象的引用类型
  5. 如何使用 Python 進行字串格式化
  6. MySQL二进制备份恢复
  7. linux(centos)环境下安装rabbitMq
  8. 用ThreadLocal来优化下代码吧
  9. 带着好奇心去探索IDEA
  10. AMD Ryzen 5000系列桌面处理器 2020年10月8日发布