搭建持续集成接口测试平台(Jenkins+Ant+Jmeter)

时间:2022-04-01 10:56:36

一、环境准备:

1、JDK:http://www.oracle.com/technetwork/java/javase/downloads/index.html

2、Jmeter:http://jmeter.apache.org/download_jmeter.cgi

3、Ant:http://ant.apache.org/bindownload.cgi

4、Jenkins:http://jenkins-ci.org/

具体可参见我另一篇博客:http://www.cnblogs.com/puresoul/p/4808416.html

二、Jemter脚本准备:

1、脚本目录:D:\B_TOOLS\apache-jmeter-2.13\demo

搭建持续集成接口测试平台(Jenkins+Ant+Jmeter)

2、脚本内容:都是简单的访问百度或google首页

baidu.jmx                      google.jmx

搭建持续集成接口测试平台(Jenkins+Ant+Jmeter) 搭建持续集成接口测试平台(Jenkins+Ant+Jmeter)

三、ANT的build.xml代码准备:

build3.xml

 <?xml version="1.0" encoding="UTF-8"?>

 <project name="ant-jmeter-test" default="run" basedir=".">
<tstamp>
<format property="time" pattern="yyyyMMddhhmm" />
</tstamp> <property environment="env"/>
<property name="ReportName" value="TestReport" />
<!-- 需要改成自己本地的 Jmeter 目录-->
<property name="jmeter.home" value="D:\B_TOOLS\apache-jmeter-2.13" />
<!-- jmeter生成jtl、html格式的结果报告的路径-->
<property name="jmeter.result.dir" value="${env.WORKSPACE}/results/${env.BUILD_ID}" />
<!-- 生成的报告的前缀-->
<property name="jmeter.result.jtlName" value="${jmeter.result.dir}/${ReportName}.jtl" />
<property name="jmeter.result.htmlName" value="${jmeter.result.dir}/${ReportName}.html" /> <target name="run">
<echo message="start..."/>
<antcall target="clean" />
<antcall target="test" />
<antcall target="report" />
</target> <target name="clean">
<mkdir dir="${env.WORKSPACE}/results/${env.BUILD_ID}" />
</target> <target name="test">
<taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
<jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}">
<!-- 声明要运行的脚本"*.jmx"指包含此目录下的所有jmeter脚本-->
<testplans dir="D:\B_TOOLS\apache-jmeter-2.13\demo" includes="*.jmx" /> <property name="jmeter.save.saveservice.output_format" value="xml"/>
</jmeter>
</target> <target name="report">
<xslt in="${jmeter.result.jtlName}"
out="${jmeter.result.htmlName}"
style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl" />
<!-- 因为上面生成报告的时候,不会将相关的图片也一起拷贝至目标目录,所以,需要手动拷贝 -->
<copy todir="${jmeter.result.dir}">
<fileset dir="${jmeter.home}/extras">
<include name="collapse.png" />
<include name="expand.png" />
</fileset>
</copy>
</target>
</project>

四、配置Jenkins Job并运行:

1、job配置如下:

搭建持续集成接口测试平台(Jenkins+Ant+Jmeter)

2、job运行结果:

 Started by user anonymous
Building in workspace D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace
[demo] $ cmd.exe /C '"ant.bat -file build3.xml run && exit %%ERRORLEVEL%%"'
Buildfile: build3.xml run:
[echo] start... clean:
[mkdir] Created dir: D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15 test:
[jmeter] Executing test plan: D:\B_TOOLS\apache-jmeter-2.13\demo\baidu.jmx ==> D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15\TestReport.jtl
[jmeter] Creating summariser <summary>
[jmeter] Created the tree successfully using D:\B_TOOLS\apache-jmeter-2.13\demo\baidu.jmx
[jmeter] Starting the test @ Tue Sep 22 15:50:33 CST 2015 (1442908233010)
[jmeter] Waiting for possible shutdown message on port 4445
[jmeter] summary + 1 in 1s = 2.0/s Avg: 254 Min: 254 Max: 254 Err: 0 (0.00%) Active: 1 Started: 1 Finished: 0
[jmeter] summary + 5 in 1s = 7.3/s Avg: 131 Min: 96 Max: 196 Err: 0 (0.00%) Active: 0 Started: 1 Finished: 1
[jmeter] summary = 6 in 1.2s = 5.0/s Avg: 151 Min: 96 Max: 254 Err: 0 (0.00%)
[jmeter] Tidying up ... @ Tue Sep 22 15:50:34 CST 2015 (1442908234310)
[jmeter] ... end of run
[jmeter] Executing test plan: D:\B_TOOLS\apache-jmeter-2.13\demo\google.jmx ==> D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15\TestReport.jtl
[jmeter] Creating summariser <summary>
[jmeter] Created the tree successfully using D:\B_TOOLS\apache-jmeter-2.13\demo\google.jmx
[jmeter] Starting the test @ Tue Sep 22 15:50:35 CST 2015 (1442908235240)
[jmeter] Waiting for possible shutdown message on port 4445
[jmeter] summary + 1 in 1s = 1.9/s Avg: 253 Min: 253 Max: 253 Err: 1 (100.00%) Active: 1 Started: 1 Finished: 0
[jmeter] summary + 5 in 1.1s = 4.4/s Avg: 209 Min: 97 Max: 420 Err: 0 (0.00%) Active: 0 Started: 1 Finished: 1
[jmeter] summary = 6 in 2s = 3.6/s Avg: 216 Min: 97 Max: 420 Err: 1 (16.67%)
[jmeter] Tidying up ... @ Tue Sep 22 15:50:36 CST 2015 (1442908236992)
[jmeter] ... end of run report:
[xslt] Processing D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15\TestReport.jtl to D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15\TestReport.html
[xslt] Loading stylesheet D:\B_TOOLS\apache-jmeter-2.13\extras\jmeter-results-detail-report_21.xsl
[copy] Copying 2 files to D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15 BUILD SUCCESSFUL
Total time: 5 seconds
[workspace] $ cmd /c call D:\Users\heman793\AppData\Local\Temp\hudson4765606743104861298.bat D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace>echo D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace
D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace>echo 15
15 D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace>cd results D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results>cd 15 D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15>exit 0
Finished: SUCCESS

3、在job的workspace目录下会生成结果报告:

搭建持续集成接口测试平台(Jenkins+Ant+Jmeter)

4、TestReport.html:

搭建持续集成接口测试平台(Jenkins+Ant+Jmeter)

更新补充:

  上面生成的报告有如下两个问题,解决办法参见Jmeter自带报告优化(一)

  1、Date report这里的时间没有正确显示出来

  2、Summary里的字段Min Time和Max Time显示的是NaN,没有显示正确的时间。

五、配置发送邮件功能

1、自已写一个发送邮件的功能并打成sendmail.jar包,放在job的workspace目录中

搭建持续集成接口测试平台(Jenkins+Ant+Jmeter)

2、jenkins增加构建步骤

  a)进入到测试报告的目录

  b) 调用sendmail.jar命令发送邮件

搭建持续集成接口测试平台(Jenkins+Ant+Jmeter)

说明:

  1、由build3.xml的第12、13行可知,报告文件生成目录为:${env.WORKSPACE}/results/${env.BUILD_ID},所以这里我要先cd到具体执行的那个build_id目录下。

  2、我们也可以把上面的两行命令写在成一个批处理文件,例如我第1步有个sendmail.bat文件就是的,然后调用时直接写sendmail.bat就好了。

另外,我本文所有脚本都在我本地,其实更好的办法是使用SVN统一管理,这里就不介绍了,感觉的可以自己研究下。