background:
Daily version of the test process, because only the interface and functional test; the line version, there will be some small bug was found missing; there will be confusion during the test phase has been tested very carefully, and wanted to look at statistical testing phase, with a scenario according to coverage develop the code is. Because we back-end code is written in java, so use jacoco; using jacocoagent + ant way; our back-end Project Description: springboot project package + + jenkins self-development tool deployment
Practical steps
1, on a remote server requires statistical coverage, deployment downloaded zip package;
1) had wanted to use wget + url download results do not support, wanted to download to the local; then spread to the remote server via scp
scp /Users/shifangfang/ydh-apk/remotecontent?filepath=org%2Fjacoco%2Fjacoco%2F0.8.3%2Fjacoco-0.8.3.zip [email protected]:/home/jacoco/
2) decompression zip package
unzip remotecontent?filepath=org%2Fjacoco%2Fjacoco%2F0.8.3%2Fjacoco-0.8.3.zip
2, modify the startup script on a remote server;
1) to prevent mistakes, to back up what the startup script
2) Stop the currently active service tomcat
3) add parameters to the startup script starup_bak.sh backed up;
-javaagent:/home/jacoco/lib/jacocoagent.jar=includes=com.qianmi.d2p.admin.api.action.chain.*,output=tcpserver,port=8044,address=172.21.33.39 -Xverify:none
such as:
Before adding startup items as:
java -Xmx1024m -Xms1024m -XX:MaxMetaspaceSize=256m -XX:MetaspaceSize=64m -Xss512k -server -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=9000 -Dcom.sun.management.jmxremote.ssl=false -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n -Duser.home=/home/tomcat -Dspring.config.location=/home/tomcat/conf/application.properties -jar /home/tomcat/bin/server.jar
After adding startup items as:
java -javaagent:/home/jacoco/lib/jacocoagent.jar=includes=com.qianmi.d2p.admin.api.action.chain.*,output=tcpserver,port=8044,address=172.21.33.39 -Xverify:none -Xmx1024m -Xms1024m -XX:MaxMetaspaceSize=256m -XX:MetaspaceSize=64m -Xss512k -server -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=9000 -Dcom.sun.management.jmxremote.ssl=false -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n -Duser.home=/home/tomcat -Dspring.config.location=/home/tomcat/conf/application.properties -jar /home/tomcat/bin/server.jar
Parameters explanation:
/Home/jacoco/lib/jacocoagent.jar--- location on the remote server jacocoagent.jar
includes = com.qianmi.d2p.admin.api.action.chain. * --- need to file statistical coverage package
output = tcpserver --- output report form
port = 8044 --- development on the test server proxy port
address = 172.21.33.39 --- test server (PS: if the words `ant dump` performed on the test server, write directly` address = 127.0.0.1`; if not to write the address of the test server)
4) to start the service and see if jacocoagent started successfully
3, the deployment ant
ant and the remote server address can be measured with a single server, it can also be separated; I is a separate server
1) Once you have downloaded ant, unzip unzip apache-ant-1.9.14-bin.zip
2) configure the environment variables vi / etc / profile
export ANT_HOME=/usr/apache-ant-1.9.14
export PATH=$PATH:$ANT_HOME/bin:$JAVA_HOME/bin
3) test whether the installation was successful ant
[root@lin-21-4-249 ~]# ant -version
Apache Ant(TM) version 1.9.14 compiled on March 12 2019
4, the source to be tested on the jar and the remote server to copy the server ant
1) upload the source code to the ant server scp d2p-admin-bff.zip [email protected]: / home /
Unzip Source: unzip d2p-admin-bff.zip
2) Upload the jar to deploy ant server, then decompressed jar package deployment: jar xvf d2p-admin-api-web-1.18.54-RELEASE.jar
4, create build.xml ant file on the server
1) Create a build.xml installation directory / bin in the ant
<?xml version="1.0" encoding="UTF-8"?>
<project name="Jacoco" xmlns:jacoco="antlib:org.jacoco.ant" default="jacoco">
<!--Jacoco 的安装路径-->
<property name="jacocoantPath" value="/home/jacoco/lib/jacocoant.jar"/>
<!--最终生成 .exec 文件的路径,Jacoco 就是根据这个文件生成最终的报告的-->
<property name="jacocoexecPath" value="/home/jacoco.exec"/>
<!--生成覆盖率报告的路径-->
<property name="reportfolderPath" value="/home/jacoco_report"/>
<!--远程 Tomcat 服务的 ip 地址-->
<property name="server_ip" value="172.21.33.39"/>
<!--前面配置的远程 Tomcat 服务打开的端口,要跟上面配置的一样-->
<property name="server_port" value="8044"/>
<!--源代码路径,源码和ant放置同一服务器上-->
<property name="checkOrderSrcPath" value="/home/d2p-admin-bff/d2p-admin-api-web/src" />
<!--.class 文件路径,部署的jar解压缩后查看待测试.class文件-->
<property name="checkOrderClasspath" value="/home/d2p_admin_api/BOOT-INF/classes/com/qianmi/d2p/admin/api/action/chain/" />
<!--让 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}">
<!-- 过滤不必要的文件 -->
<exclude name="**/R.class"/>
<exclude name="**/R$*.class"/>
<exclude name="**/*$ViewInjector*.*"/>
<exclude name="**/BuildConfig.*"/>
<exclude name="**/Manifest*.*"/>
</fileset>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${checkOrderSrcPath}" />
</sourcefiles>
</group>
</structure>
<html destdir="${reportfolderPath}" encoding="UTF-8" />
<csv destfile="${reportfolderPath}/coverage-report.csv" encoding="UTF-8"/>
<xml destfile="${reportfolderPath}/coverage-report.xml" encoding="UTF-8"/>
</jacoco:report>
</target>
</project>
5、执行测试后,下载报告
1)在ant所在的服务器bin下
ant dump
ant bulid
ant report
6、把生成的report上传到本地
1) ant host server, the previous report build.xml path: / home / jacoco_report
2)scp /home/jacoco_report/coverage-report.xml ~/Desktop
Reference documents: https://www.jianshu.com/p/639e51c76544
https://www.jianshu.com/p/b6a7ab72eb2d
On subsequent integration into jenkins, previously recorded
Reproduced in: https: //www.jianshu.com/p/fe5144c63d0d