当前位置:网站首页>快速使用 Jacoco 代码覆盖率统计
快速使用 Jacoco 代码覆盖率统计
2022-07-07 04:31:00 【Wu_Candy】
简介
Jacoco 是一个开源的覆盖率工具。
Jacoco 可以嵌入到 Ant 、Maven 中,并提供了 EclEmma Eclipse 插件,也可以使用 JavaAgent技术监控 Java 程序。
很多第三方的工具提供了对 Jacoco 的集成,如 sonar、Jenkins 等。
今天分享使用的环境是:
k8s+jenkins 构建的,其中涉及到了在 node 节点机的容器中生成 jacoco.exec 文件,然后将其拷贝到节点机,再从节点机将 jacoco.exec 文件拷贝到 jenkins 的代码库中,用于生成报告。
操作步骤
**Step1:**引入相应的 jar 到应用服务的目录下,并修改需要监测的应用服务的启动脚本
以下是在 jenkins 中将相应应用服务部署脚本进行了调整
echo ' cd /opt/ wget http://192.168.214.100:7777/jacoco/tools/jacocoagent.jar #此处启动了一个siampleHttp 服务,用于获取jar 包 wget http://192.168.214.100:7777/jacoco/tools/jacocoant.jar wget -O buildexec.xml http://192.168.214.100:7777/jacoco/buildexec/build.xml wget http://192.168.214.100:7777/jacoco/tools/cron.sh sh cron.sh& #此处见step2 中编辑的脚本 sed -i "s#exec java#exec java -javaagent:/opt/jacocoagent.jar=includes=*,output=tcpserver,port=8044,address=127.0.0.1 -Xverify:none -javaagent:/opt/skywalking-agent/skywalking-agent.jar #g" /opt/mt/bin/startup.sh && cd /opt/mt/ && dos2unix -k start.sh && cd /opt/mt/bin/ && dos2unix -k startup.sh && sh startup.sh '> ./start.sh
Step2: 用于在容器里定时生成 jacoco.exec
此 jacoco.exec 文件是拿取的代码执行的情况文件所生成的一个 exec文件,该文件仅仅是 Step1 中需要的一个脚本文件。
[[email protected] tools]# more cron.sh
while true;do ant dump -buildfile buildexec.xml;sleep 60;done
Step3: 在 jenkins 拉取源码处,将生成的 jacoco.exec 文件复制过来,然后生成最终的执行报告
以下是一个jenkins job
cd ../test-deploy
wget -O buildreport.xml http://192.168.214.100:7777/jacoco/buildreport/build_test_model.xml
wget -nc http://192.168.214.100:7777/jacoco/tools/jacocoant.jar
wget -nc http://192.168.214.100:7777/jacoco/tools/dlexec.sh
sh dlexec.sh testqyd test-model #详细见step4
ant report -buildfile ./buildreport.xml #生成执行的报告
open site:http://192.168.214.100:7777/jacoco/report/testqyd/test-model/jacocoReport/ #打开报告
Step4: 从 docker 中获取jacoco.exec文件,然后放在jenkins摘取源码的目录下
[[email protected]]# more dlexec.sh
platformname=$1
projectname=$2
echo " ############################################# # 从node节点机下载jacoco.exec文件执行脚本 # ############################################# "
case $# in
0)
echo ""
;;
1)
ssh [email protected] "kubectl get pods -n $platformname -o wide"
;;
2)
pnip=`ssh 192.168.214.50 "kubectl --kubeconfig=/etc/kubernetes/kubelet.kubeconfig get pods -n $platformname -o wide|grep $projectname|awk '{print \\$7}'|head -1"`
if [ $pnip == '192.168.214.55' ];then
pnip='[email protected]'
rqid=`ssh $pnip "docker ps |grep $platformname/$projectname|awk '{print \\$1}'|head -1"`
ssh $pnip "docker cp $rqid:/opt/jacoco.exec /opt/test/jacoco.exec"
scp $pnip:/opt/test/jacoco.exec ./
elif [ $pnip == '192.168.214.56' ];then
pnip='[email protected]'
rqid=`ssh $pnip "docker ps |grep $platformname/$projectname|awk '{print \\$1}'|head -1"`
ssh $pnip "docker cp $rqid:/opt/jacoco.exec /opt/test/jacoco.exec"
scp $pnip:/opt/test/jacoco.exec ./
else
echo "not found!"
fi
;;
esac
Jenkins 执行效果图如下图所示:

欢迎关注:无量测试之道公众号,回复: 领取资源
Python+Unittest框架API自动化、
Python+Unittest框架API自动化、
Python+Pytest框架API自动化、
Python+Pandas+Pyecharts大数据分析、
Python+Selenium框架Web的UI自动化、
Python+Appium框架APP的UI自动化、
Python编程学习资源干货、
Vue前端组件化框架开发、
资源和代码 免费送啦~
包含:数据分析、大数据、机器学习、测试开发、API接口自动化、测试运维、UI自动化、性能测试、代码检测、编程技术等。
微信搜索公众号: 无量测试之道,添加关注,让我们一起共同成长 !
边栏推荐
- [SUCTF 2019]Game
- SQL优化的魅力!从 30248s 到 0.001s
- buuctf misc USB
- Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
- php导出百万数据
- 为什么要了解现货黄金走势?
- Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
- Is the test cycle compressed? Teach you 9 ways to deal with it
- 3D reconstruction - stereo correction
- Tianqing sends instructions to bypass the secondary verification
猜你喜欢

Common validation comments
![[ANSYS] learning experience of APDL finite element analysis](/img/bc/dc0742c308816553a80d50d1a990e3.jpg)
[ANSYS] learning experience of APDL finite element analysis
![[CV] Wu Enda machine learning course notes | Chapter 8](/img/c0/7a39355fb3a6cb506f0fbcf2a7aa24.jpg)
[CV] Wu Enda machine learning course notes | Chapter 8

3D reconstruction - stereo correction

Problem solving: unable to connect to redis

idea添加类注释模板和方法模板

Leetcode-543. Diameter of Binary Tree

resource 创建包方式

Explore Cassandra's decentralized distributed architecture
![[2022 ciscn] replay of preliminary web topics](/img/1c/4297379fccde28f76ebe04d085c5a4.png)
[2022 ciscn] replay of preliminary web topics
随机推荐
ASEMI整流桥RS210参数,RS210规格,RS210封装
The metauniverse of the platofarm farm continues to expand, with Dao governance as the core
[CV] Wu Enda machine learning course notes | Chapter 8
Detailed explanation of uboot image generation process of Hisilicon chip (hi3516dv300)
面试结束后,被面试官在朋友圈吐槽了......
leetcode:105. 从前序与中序遍历序列构造二叉树
nacos
buuctf misc USB
nacos
Idea add class annotation template and method template
KBU1510-ASEMI电源专用15A整流桥KBU1510
Button wizard collection learning - mineral medicine collection and running map
Ansible
pytorch 参数初始化
SQL优化的魅力!从 30248s 到 0.001s
1140_ SiCp learning notes_ Use Newton's method to solve the square root
buuctf misc USB
图解GPT3的工作原理
Leetcode-543. Diameter of Binary Tree
What is the interval in gatk4??