从0到1,用Jmeter搭建HTTP接口自动化引擎 1.0 版本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从0到1,用Jmeter搭建HTTP接口自动化引擎 1.0 版本相关的知识,希望对你有一定的参考价值。
参考技术A如图:
附件内容包括
(1)测试用例执行摘要:
(2)测试用例详细信息:
如果http请求是post
获取post请求数据
断言内容解析:
int errcount = Integer.parseInt(vars.get("errcount").trim());
int allcount = Integer.parseInt(vars.get("respcount").trim());
int equalscount=0;
int equalsokcount=0;
if("$HttpStatus"!="")
// log.info("==========================$HttpStatus");
if(SampleResult.getResponseCode().equals("$HttpStatus".trim()))
equalscount++;
equalsokcount++;
// log.info("================equalscount: " + equalscount);
// log.info("================equalsokcount: " + equalsokcount);
else
String code = SampleResult.getResponseCode();
vars.put("respcode",code);
vars.put("AssertionNoOk","断言失败内容为:HttpStatus,实际请求返回的HttpStatus为: "+code+",断言内容的HttpStatus为: "+"$HttpStatus");
equalscount++;
// log.info("================ERRORequalscount: " + equalscount);
// log.info("================ERRORequalsokcount: " + equalsokcount);
// log.info("====================ErrorHttpStatus is: " + code);
if("$ResponseMessage"!="")
if(SampleResult.getResponseMessage().equalsIgnoreCase("$ResponseMessage".trim()))
equalscount++;
equalsokcount++;
else
equalscount++;
String msg = SampleResult.getResponseMessage();
vars.put("respmsg",msg);
vars.put("AssertionNoOk","断言失败内容为:ResponseMessage,实际请求返回的ResponseMessage为: "+msg+",断言内容的ResponseMessage为: "+"$ResponseMessage");
if("$ResponseData1"!="")
if(SampleResult.getResponseDataAsString().contains("$ResponseData1".trim()))
equalscount++;
equalsokcount++;
else
equalscount++;
String data = SampleResult.getResponseDataAsString();
vars.put("respdata",data);
vars.put("AssertionNoOk","断言失败内容为:ResponseData1,实际请求返回的ResponseData1为: "+data+",断言内容的ResponseData1为: "+"$ResponseData1");
//log.info("===================================ResponseData2=" + "$ResponseData2".trim());
if("$ResponseData2"!="")
if(SampleResult.getResponseDataAsString().contains("$ResponseData2".trim()))
equalscount++;
equalsokcount++;
else
equalscount++;
String data = SampleResult.getResponseDataAsString();
vars.put("respdata",data);
vars.put("AssertionNoOk","断言失败内容为:ResponseData2,实际请求返回的ResponseData2为: "+data+",断言内容的ResponseData2为: "+"$ResponseData2");
if("$ResponseData3"!="")
if(SampleResult.getResponseDataAsString().contains("$ResponseData3".trim()))
equalscount++;
equalsokcount++;
else
equalscount++;
String data = SampleResult.getResponseDataAsString();
vars.put("respdata",data);
vars.put("AssertionNoOk","断言失败内容为:ResponseData3,实际请求返回的ResponseData3为: "+data+",\\n断言内容的ResponseData3为: "+"$ResponseData3");
if(equalscount==equalsokcount)
SampleResult.setSuccessful(true);
vars.put("sendemail","no");
allcount++;
vars.put("respcount",allcount.toString());
else
errcount++;
allcount++;
SampleResult.setSuccessful(false);
vars.put("errcount",errcount.toString());
vars.put("respcount",allcount.toString());
vars.put("sendemail","yes");
利用SMTP Sampler发送邮件
邮件内容如下:
至此,Jmeter HTTP接口自动化引擎搭建完毕
还可以利用jenkins定时执行Jmeter引擎,从而实现对线上接口的监控
资料参考:
(1)Jmeter build.xml文件: http://pan.baidu.com/s/1i46tMlR
(2)Jmeter生成报告格式(参考heman贺满博客 http://www.cnblogs.com/puresoul/http://pan.baidu.com/s/1kVssMzl ):
(3)jmeter.properties(修改jmeter生成.jtl文件内容配置): http://pan.baidu.com/s/1cyaNlc
(4)测试用例模版:http://pan.baidu.com/s/1kVegq6j
(5)Jmeter接口自动化引擎内容:http://pan.baidu.com/s/1kVop9n5
Jmeter3.1AntJenkins接口自动化平台搭建步骤;所遇问题及解决方案
Win下Jenkins+Jmeter+Ant自动化集成环境搭建
环境:win10
涉及:Jdk1.7.0_80、Jenkins2.60.3、Jmeter3.1、Ant1.9.9
步骤:
1. Java下载、安装、配置环境变量、验证
l 安装:默认
l 配置:JAVA_HOME:C:Program FilesJavajdk1.7.0_80
l 配置:Path:增加%JAVA_HOME%in;
l 验证:win+R输入cmd进入cmd.exe,执行javac -version,显示java版本即表示jdk安装成功
2. Jmeter下载、安装、配置环境变量、验证
l 下载:http://archive.apache.org/dist/jmeter/binaries/
l 解压:默认
l 配置:JMETER_HOME:D: 1Test 2Jmeterapache-jmeter-3.1
l 配置:
CLASSPATH:%JMETER_HOME%libextApacheJMeter_core.jar;%JMETER_HOME%libjorphan.jar;%JMETER_HOME%liblogkit-2.0.jar;
l 启动:进入apache-jmeter-3.1in下启动jmeter.bat,jmeter启动成功即为安装成功
3. Ant下载、配置环境变量、验证
l 下载:http://ant.apache.org/bindownload.cgi
l 解压:默认
l 配置:ANT_HOME:D: 1Test 2Jmeterant
l 配置:CLASSPATH:增加%ANT_HOME%lib;
l 配置:Path:增加%ANT_HOME%in;
l 验证:win+R输入cmd进入cmd.exe,进入ant目录,执行ant -version,显示版本即表示安装成功
4. 修改jmeter.properties
修改apache-jmeter3.1in下的jmeter.properties
# assertion_results_failure_message only affects CSV output
jmeter.save.saveservice.assertion_results_failure_message=true
#
# legitimate values: none, first, all
#jmeter.save.saveservice.assertion_results=none
#
jmeter.save.saveservice.data_type=true
jmeter.save.saveservice.label=true
jmeter.save.saveservice.response_code=true
#response_data is not currently supported for CSV output
jmeter.save.saveservice.response_data=true
#Save ResponseData for failed samples
jmeter.save.saveservice.response_data.on_error=false
jmeter.save.saveservice.response_message=true
jmeter.save.saveservice.successful=true
jmeter.save.saveservice.thread_name=true
jmeter.save.saveservice.time=true
jmeter.save.saveservice.subresults=true
#jmeter.save.saveservice.assertions=true
jmeter.save.saveservice.latency=true
jmeter.save.saveservice.connect_time=true
jmeter.save.saveservice.samplerData=true
jmeter.save.saveservice.responseHeaders=false
jmeter.save.saveservice.requestHeaders=true
jmeter.save.saveservice.encoding=false
jmeter.save.saveservice.bytes=true
# Only available with HttpClient4
jmeter.save.saveservice.sent_bytes=true
jmeter.save.saveservice.url=false
jmeter.save.saveservice.filename=false
jmeter.save.saveservice.hostname=true
jmeter.save.saveservice.thread_counts=true
jmeter.save.saveservice.sample_count=false
jmeter.save.saveservice.idle_time=true
5. 准备好脚本,例如存放在D: 1Test 2Jmeterscripts est
6. 将apache-jmeter-3.1extras下的ant-jmeter-1.1.1.jar放在antlib下
7. Ant配置build.xml
l 将apache-jmeter3.1extras中的build.xml复制到antin下,并进行配置保存。
<?xml version="1.0" encoding="GBK"?>
<!-- 该配置文件作用为使用Ant来批量构建jmeter下loadtests文件夹里面的所有脚本,并按照指定格式来生成报告-->
<project name="ant-jmeter-test" default="all" basedir="D: 1Test 2Jmeterapache-jmeter-3.1">
<tstamp>
<format property="time" pattern="2017-09-01" />
</tstamp>
<!-- 生成的报告的前缀和定义结果集的相关参数-->
<property name="ReportName" value="TestReport" />
<!-- 本地Jmeter目录-->
<property name="jmeter.home" value="D: 1Test 2Jmeterapache-jmeter-3.1" />
<!-- 应生成的jtl和html报告的路径-->
<property name="jmeter.result.jtl.dir" value="D: 1Test 2Jmeterscripts est" />
<property name="jmeter.result.html.dir" value="D: 1Test 2Jmeterscripts est"/>
<target name="all">
<antcall target="test" />
<antcall target="report" />
</target>
<!-- 测试脚本-->
<target name="test">
<taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
<!-- jtl应存放路径和名称-->
<jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtl.dir}jmeter_result.jtl">
<!-- 要运行的脚本。"*.jmx"指 包含此目录下的所有jmeter脚本-->
<testplans dir="${jmeter.result.jtl.dir}" includes="*.jmx" />
<property name="jmeter.save.saveservice.output_format" value="xml"/>
<property name="jmeter.save.saveservice.assertion_results" value="all"/>
<property name="jmeter.save.saveservice.bytes" value="true"/>
<property name="file_format.testlog" value="${format}"/>
<property name="jmeter.save.saveservice.response_data.on_error" value="${funcMode}"/>
</jmeter>
</target>
<!-- 生成报告 jtl路径和名称、html路径和名称 如需要多生成报告,则对写几个xslt即可-->
<target name="report">
<!--年月日时分秒时间戳 格式可修改-->
<tstamp prefix="build.">
<format property="TimeSign" pattern="yyyyMMddHHmmss"/>
</tstamp>
<xslt
in="${jmeter.result.jtl.dir}jmeter_result.jtl"
<!--报告名称带有年月日时分秒-->
out="${jmeter.result.html.dir}jmeter_DetailsReport-${build.TimeSign}.html"
style="${jmeter.home}extrasjmeter.results.shanhe.me.xsl" />
<xslt
in="${jmeter.result.jtl.dir}jmeter_result.jtl"
<!--报告名称带有年月日-->
out="${jmeter.result.html.dir}jmeter_SummaryReport-${DSTAMP}.html"
style="${jmeter.home}extrasjmeter-results-report_21.xsl" />
<!-- 注意生成报告的路径-->
<!-- 生成报告时,不会将相关的图片自动拷贝至目标目录,所以,需要通过代码实现 -->
<copy todir="${jmeter.result.html.dir}">
<fileset dir="${jmeter.home}extras">
<include name="collapse.png" />
<include name="expand.png" />
</fileset>
</copy>
</target>
</project>
l win+R输入cmd进入cmd.exe,执行ant -buildfile build.xml,即可成功。
8. Jenkins下载、安装、启动
l 下载:https://jenkins.io/download/,下载左边稳定版
l 安装:默认
l 登录localhost:8080,首次登录用户名为administrator,密码为提示文件中的内容,复制过来即可登录,登录后设置自己的用户名和密码即可
l 插件:首页->系统设置->管理插件->可选插件,搜索Performance plugin(Jmeter报告需要),安装此插件;
l Jenkins相关配置参考:
http://blog.csdn.net/qi_lin7/article/details/52045713
http://www.cnblogs.com/chenchen-tester/p/6930200.html
以前走的弯路,及遇到的错误问题
1、如提示错误:2 字节 UT8-8 序列的字节 2无效,则将build文件用notepad++打开,在编码选择“以UTF-8无BOM格式编码”,然后保存。
2、java(TM) platform se binary 已停止工作
build配置文件按如下网页修改
http://www.cnblogs.com/test2016/p/5342065.html
3、${lib.dir} does not exist.
将以下内容注释掉:
<path id="xslt.classpath">
<fileset dir="${lib.dir}" includes="xalan*.jar"/>
<fileset dir="${lib.dir}" includes="serializer*.jar"/>
</path>
4、The following error occurred while executing this line:
解决办法:未确认
Variable name: _JAVA_OPTIONS
Variable value: -Xmx512M
5、jmeter jar file not found or not a valid file:
D: 1Test 2JmeterinApacheJMeter.jar
解决:Jmeter路径不对
6、java.lang.IllegalAccessError:
tried to access class org.apache.xml.serializer.ExtendedContentHandler from class org.apache.xalan.transformer.TransformerImpl
ant下lib下有xalan*.jar,serializer*.jar,删除即可
7、The <copy> type doesn‘t support nested text data ("86 90").
可能是粘贴过去的内容不兼容,重新编写一遍即可
以上是关于从0到1,用Jmeter搭建HTTP接口自动化引擎 1.0 版本的主要内容,如果未能解决你的问题,请参考以下文章
接口自动化测试环境搭建jmeter+ant+git+jenkins
centos7搭建ANT+jmeter+jenkins接口测试自动化环境