前提
自动化代码和应用服务代码分离。jenkins和tomcat服务器分离
思想
1.在tomcat启动javaagent监听。
2.运用其他job_B已部署的应用服务代码
3.拉取自动化代码,开始测试
4.测试完成后,调用ant,生成覆盖率的exec文件
5.拉取应用服务代码到当前job_a的workspace
6.使用jacoco coverage report统计覆盖率
步骤
1.jenkins安装插件
2.进入系统管理,配置maven和ant
3.新建maven风格的job
4.自动化测试代码git配置
5.build输入clean test
6.执行shell
cd /Users/qiaojiafei/.jenkins/workspace/qingzhutool
git pull
mvn compile
7.调用ant,targets输入dump,ant的配置文件如下:
<?xml version="1.0" ?> <project name="qjf" xmlns:jacoco="antlib:org.jacoco.ant" default="jacoco"> <property environment="env"/> <!--JaCoCo的安装路径--> <property name="jacocoantPath" value="/Users/qiaojiafei/Documents/work/soft/jacoco-0.8.3/lib/jacocoant.jar"/> <!--最终生成.exec文件的路径,JaCoCo就是根据这个文件生成最终的报告的--> <property name="jacocoexecPath" value="/Users/qiaojiafei/.jenkins/workspace/${env.JOB_NAME}/jacoco.exec"/> <!--生成覆盖率报告的路径--> <property name="reportfolderPath" value="/Users/qiaojiafei/.jenkins/workspace/${env.JOB_NAME}/report/"/> <!--远程tomcat服务或者应用的ip地址--> <property name="server_ip" value="127.0.0.1"/> <!--前面配置的远程tomcat服务或者应用打开的端口,要跟上面配置的一样--> <property name="server_port" value="6300"/> <!--源代码路径--> <property name="checkOrderSrcpath" value="/Users/qiaojiafei/.jenkins/workspace/${env.JOB_NAME}/qingzhutool" /> <!--.class文件路径--> <property name="checkOrderClasspath" value="/Users/qiaojiafei/.jenkins/workspace/qingzhutool/target/classes" /> <!--让ant知道去哪儿找JaCoCo--> <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml"> <classpath path="${jacocoantPath}" /> </taskdef> <!--dump任务: 根据前面配置的ip地址,和端口号, 访问目标tomcat服务或者应用,并生成.exec文件。--> <target name="dump"> <jacoco:dump address="${server_ip}" reset="false" destfile="${jacocoexecPath}" port="${server_port}" append="true"/> </target> <!--JaCoCo任务: 根据前面配置的源代码路径和.class文件路径, 根据dump后,生成的.exec文件,生成最终的html覆盖率报告。--> <target name="report"> <delete dir="${reportfolderPath}" /> <mkdir dir="${reportfolderPath}" /> <jacoco:report> <executiondata> <file file="${jacocoexecPath}" /> </executiondata> <structure name="JaCoCo Report"> <group name="Check Order related"> <classfiles> <fileset dir="${checkOrderClasspath}" /> </classfiles> <sourcefiles encoding="gbk"> <fileset dir="${checkOrderSrcpath}" /> </sourcefiles> </group> </structure> <html destdir="${reportfolderPath}" encoding="utf-8" /> </jacoco:report> </target> </project>
8.再次执行shell
#cp /Users/qiaojiafei/.jenkins/workspace/jacocotesttool/jacoco.exec /Users/qiaojiafei/.jenkins/workspace/qingzhutool/
rm -rf qingzhutool
ls
git clone git@192.144.141.166:qingzhuhotel/qingzhutool.git
cd qingzhutool
git checkout master
git pull
#cp /Users/qiaojiafei/.jenkins/workspace/qingzhutool/jacoco.exec ./
mvn compile
9.最后构建后添加record jacoco coverage report
总体配置图如下