转载地址:https://testerhome.com/topics/2580
一、什么是接口测试?
接口测试是测试系统组件间接口的一种测试。接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点。测试的重点是要检查数据的交换,传递和控制管理过程,以及系统间的相互逻辑依赖关系等。
接口测试适用于为其他系统提供服务的底层框架系统和中心服务系统,主要测试这些系统对外部提供的接口,验证其正确性和稳定性。接口测试同样适用于一个上层系统中的服务层接口,越往上层,其测试的难度越大。
接口测试实施在多系统多平台的构架下,有着极为高效的成本收益比,接口测试天生为高复杂性的平台带来高效的缺陷监测和质量监督能力。平台越复杂,系统越庞大,接口测试的效果越明显。
基于接口测试的重要性,以及它比较容易自动化的特性,通过持续集成的接口监控能够及时的发现项目中存在的问题,这对持续运营的项目来说,非常重要。
二、接口测试的流程
1、 项目启动后,测试人员要尽早找到开发人员拿到接口测试文档
2、 获取接口测试文档后,就可以进行接口用例的编写和调试
3、 接口用例编写调试完成后,部署到持续集成的测试环境中,
4、 设定脚本运行频率,告警方式等基本参数,进行接口的日常监控
5、 每日进行接口脚本的维护更新,接口异常的处理
三、编写接口测试脚本
大部分性能工具都可以用来进行接口测试,jmeter就是一个好用的性能测试工具,他也同样可以用来进行接口测试,jmeter比较适用于CGI、webservice、DB等类型的接口测试。下面以websevice api接口为例说明如何编写接口测试用例(本文侧重于接口测试平台的搭建,对于具体工具是使用只进行简单介绍,对于工具不了解的,可以自行百度):
以下是我弄的一个简单的脚本。
每个请求都加一个响应断言来判断是否达到期望。
如果用命令来执行,也很简单:
jmeter -n -t test.jmx -l test.jtl
四、接口持续集成
在这块卡了好久,主要是report展示。之前是不想再用其他工具(如:Ant, Maven ), 就想将jtl文件的报告展现出来,弄了很久没成功。最后还是向工具屈服,用的Ant来编译.
主要是配置build.xml
一个示例:
<code><span class="cp"><?xml version="1.0" encoding="UTF-8"?></span> <span class="nt"><project</span> <span class="na">name=</span><span class="s">"ant-jmeter-test"</span> <span class="na">default=</span><span class="s">"all"</span> <span class="na">basedir=</span><span class="s">"."</span><span class="nt">></span> <span class="nt"><tstamp></span> <span class="nt"><format</span> <span class="na">property=</span><span class="s">"time"</span> <span class="na">pattern=</span><span class="s">"yyyyMMddhhmm"</span> <span class="nt">/></span> <span class="nt"></tstamp></span> <span class="c"><!-- 需要改成自己本地的 Jmeter 目录--></span> <span class="nt"><property</span> <span class="na">name=</span><span class="s">"jmeter.home"</span> <span class="na">value=</span><span class="s">"F:\xxxxx\Jmeter"</span> <span class="nt">/></span> <span class="c"><!-- jmeter生成jtl格式的结果报告的路径--></span> <span class="nt"><property</span> <span class="na">name=</span><span class="s">"jmeter.result.jtl.dir"</span> <span class="na">value=</span><span class="s">"F:\xxxxx\resultLog\jtl"</span> <span class="nt">/></span> <span class="c"><!-- jmeter生成html格式的结果报告的路径--></span> <span class="nt"><property</span> <span class="na">name=</span><span class="s">"jmeter.result.html.dir"</span> <span class="na">value=</span><span class="s">"F:\xxxxx\resultLog\html"</span> <span class="nt">/></span> <span class="c"><!-- 生成的报告的前缀-> <property name="ReportName" value="TestReport" /> <property name="jmeter.result.jtlName" value="${jmeter.result.jtl.dir}/${ReportName}${time}.jtl" /> <property name="jmeter.result.htmlName" value="${jmeter.result.html.dir}/${ReportName}${time}.html" /> <target name="all"> <antcall target="test" /> <antcall target="report" /> </target> <target name="test"> <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" /> <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}"> <!-- 声明要运行的脚本。"*.jmx"指包含此目录下的所有jmeter脚本--></span> <span class="nt"><testplans</span> <span class="na">dir=</span><span class="s">"D:\test_case_path"</span> <span class="na">includes=</span><span class="s">"*.jmx"</span> <span class="nt">/></span> <span class="nt"></jmeter></span> <span class="nt"></target></span> <span class="nt"><target</span> <span class="na">name=</span><span class="s">"report"</span><span class="nt">></span> <span class="nt"><xslt</span> <span class="na">in=</span><span class="s">"${jmeter.result.jtlName}"</span> <span class="na">out=</span><span class="s">"${jmeter.result.htmlName}"</span> <span class="na">style=</span><span class="s">"${jmeter.home}/extras/jmeter-results-detail-report_21.xsl"</span> <span class="nt">/></span> <span class="c"><!-- 因为上面生成报告的时候,不会将相关的图片也一起拷贝至目标目录,所以,需要手动拷贝 --></span> <span class="nt"><copy</span> <span class="na">todir=</span><span class="s">"${jmeter.result.html.dir}"</span><span class="nt">></span> <span class="nt"><fileset</span> <span class="na">dir=</span><span class="s">"${jmeter.home}/extras"</span><span class="nt">></span> <span class="nt"><include</span> <span class="na">name=</span><span class="s">"collapse.png"</span> <span class="nt">/></span> <span class="nt"><include</span> <span class="na">name=</span><span class="s">"expand.png"</span> <span class="nt">/></span> <span class="nt"></fileset></span> <span class="nt"></copy></span> <span class="nt"></target></span> <span class="nt"></project></span></code>
Jenkins 上安装 “Publish performance test result report” ,“Publish HTML reports” 的插件,配置一下。
运行。
结果如下:
如果加上一些并发,负载等,就是性能测试报告了。
性能报告的曲线展示还没弄出来,有空再看看。
补上性能测试报告:
之前一直出了个错:
<code>Performance: Recording JMeter reports '**/*.jtl' Performance: no JMeter files matching '**/*.jtl' have been found. Has the report generated?. Setting Build to FAILURE Build step 'Publish Performance test result report' changed build result to FAILURE Finished: FAILURE</code>
导致结果出不来。后面在 Jmeter/bin 下将 jmeter.properties 中的:
jmeter.save.saveservice.output_format=csv
改成:
jmeter.save.saveservice.output_format=xml
结果就是这样的:
特别说明,里面用到的jar包自行下载并且替换路径
<?xml version="1.0" encoding="utf-8"?>
<project default="all">
<!-- Define your Jmeter Home & Your Report Title & Interval Time Between Test-->
<property name="report.title" value="数据中心接口测试报告"/>
<property name="jmeter-home" location="/home/xiongjinfei/ProgramFiles/apache-jmeter-2.11" />
<property name = "interval-time-in-seconds" value ="10"/>
<!-- default path config, you can modify for your own requirement;Generally, you do not need to modify -->
<property environment="env" />
<property name="runremote" value="false"/>
<property name="resultBase" value="results"/>
<property name="results.jtl" value="jtl"/>
<property name="results.html" value ="html"/>
<property name="jmxs.dir" value= "jmxs"/>
<tstamp><format property="report.datestamp" pattern="yyyy-MM-dd-HH-mm-ss"/></tstamp>
<property name="time" value="${report.datestamp}"/>
<!-- Diffrent version of Jmeter has its own ant-jmeter.jar,Please input the right versioin -->
<path id="ant.jmeter.classpath">
<pathelement location="${jmeter-home}/lib/ant-jmeter-1.1.1.jar" />
</path>
<taskdef name="jmeter"
classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"
classpathref="ant.jmeter.classpath" />
<!-- just to support foreach by ant -->
<taskdef resource="net/sf/antcontrib/antlib.xml" >
<classpath>
<pathelement location="/usr/share/java/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
<!-- use this config to generate html report; if not, may not display Min/Max Time in html-->
<path id="xslt.classpath">
<fileset dir="/home/xiongjinfei/work/CITEST/libs" includes="xalan-2.7.1.jar"/>
<fileset dir="/home/xiongjinfei/work/CITEST/libs" includes="serializer-2.9.1.jar"/>
</path>
<!--运行之前首先创建临时结果文件夹-->
<target name="create-folder">
<delete dir="${resultBase}/temp"/>
<mkdir dir="${resultBase}/temp/${results.jtl}" />
<mkdir dir="${resultBase}/temp/${results.html}" />
</target>
<target name="all-test" depends="create-folder">
<foreach param="jmxfile" target="test" >
<fileset dir="${jmxs.dir}">
<include name="*.jmx" />
</fileset>
</foreach>
</target>
<target name="test" >
<basename property="jmx.filename" file="${jmxfile}" suffix=".jmx"/>
<echo message="---------- Processing ${jmxfile} -----------"/>
<echo message="resultlogdir===${resultBase}/temp/${results.jtl}"/>
<jmeter
jmeterhome="${jmeter-home}"
resultlogdir="${resultBase}/temp/${results.jtl}"
runremote="${runremote}"
resultlog="${jmx.filename}.jtl"
testplan="${jmxs.dir}/${jmx.filename}.jmx">
<jvmarg value="-Xincgc"/>
<jvmarg value="-Xms1024m"/>
<jvmarg value="-Xmx1024m"/>
<property name="jmeter.save.saveservice.output_format" value="xml"/>
</jmeter>
<sleep seconds="3"></sleep>
<!--Generate html report-->
<xslt in="${resultBase}/temp/${results.jtl}/${jmx.filename}.jtl"
out="${resultBase}/temp/${results.html}/${jmx.filename}.html" classpathref="xslt.classpath"
style="${jmeter-home}/extras/jmeter-results-detail-report_21.xsl" >
<param name="dateReport" expression="${report.datestamp}"/>
<param name="showData" expression="n"/>
<param name="titleReport" expression="${report.title}:[${jmx.filename}]"/>
</xslt>
<echo message="跑完了一个脚本,让我休息一下吧!Sleep ${interval-time-in-seconds} Seconds, yo-yo-check-now,开始工作 Please waiting ......"/>
<sleep seconds="${interval-time-in-seconds}"></sleep>
</target>
<target name="copy-images" depends="all-test">
<copy file="${jmeter-home}/extras/expand.png" tofile="${results.html}/expand.png"/>
<copy file="${jmeter-home}/extras/collapse.png" tofile="${results.html}/collapse.png"/>
<copydir src="${resultBase}/temp" dest="${resultBase}/${report.datestamp}"/>
<copydir src="${results.html}" dest="${resultBase}/${report.datestamp}/html"/>
<delete dir="${resultBase}/temp"/>
</target>
<target name="all" depends="all-test, copy-images" />
</project>