9.1 安装和介绍
JMeter 安装文件路径:
JMeter 是轻量级的开源且稳定的自动化测试工具。
思路:在接口说明文档中整理出接口测试案例,其中需要包括详细的入参和出参 数据,以及明确的格式和检查点,做到接口用例 100%覆盖,并和开发人员一起对接 口测试案例进行评审。
9.1.1 安装 JDK 并配置环境变量
安装 JDK,配置 JDK 环境变量。在系统 cmd 命令提示符下输入 java -version, 如果能够看到 Java 版本信息,说明 JDK 安装成功。解压缩 Apache-jmeter-2.12.zip 到 E 盘目录下 E:\apiauto\apache-jmeter-2.12,如图 9.1 所示。
▲图 9.1
设置 JMeter 环境变量,运行 JMeter,直接打开 E:\apache-jmeter-2.12\bin\jmeter.bat 就可以了。
9.3 结合 Ant 持续集成到 Jenkins
-----------------------
把 Ant 及 9.4 节中的接口测试结果报告结合起来。
Jenkins的安装配置集成,请参见第11节。
xml 文件中一一对应配置文件 名、文件路径和各字段。
Build.xml:
<?xml version="1.0"?>
<project name="ant-jmeter" default="all"> <description>
</description>
<property name="testpath" value="${user.dir}"/>
<!—构建路径设置-->
<property name="resultpath" value="/Users/zouhui/Documents/workspace"/>
<property name="jmeter.home" value="${basedir}/.."/>
<property name="report.title" value="接口测试结果:"/>
<!-- Name of test (without .jmx) --> <property name="test" value="Backapp"/>
<!-- Should report include response data for failures? --> <property name="show-data" value="n"/>
<property name="format" value="2.1"/>
<condition property="style_version" value="">
<equals arg1="${format}" arg2="2.0"/>
</condition>
<condition property="style_version" value="_21">
<equals arg1="${format}" arg2="2.1"/>
</condition>
<condition property="funcMode">
<equals arg1="${show-data}" arg2="y"/>
</condition>
<condition property="funcMode" value="false"> <not>
<equals arg1="${show-data}" arg2="y"/> </not>
</condition>
<!-- Allow jar to be picked up locally -->
<path id="jmeter.classpath"> <!—编译路径设置-->
<fileset dir="${basedir}/extras">
<include name="ant-jmeter*.jar"/>
</fileset>
</path>
<taskdef name="jmeter"
classpathref="jmeter.classpath" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/>
<target name="all" depends="run,report"/>
<target name="run"> <!—运行测试设置-->
<echo>funcMode = ${funcMode}</echo>
<delete file="${testpath}/extras/${test}.html"/>
<delete file="${testpath}/extras/${test}.jtl"/> <jmeter
jmeterhome="${jmeter.home}/apiTest" testplan ="${testpath}/extras/${test}.jmx"
resultlog="${testpath}/extras/${test}.jtl">
<!--
<jvmarg value="-Xincgc"/>
<jvmarg value="-Xmx128m"/>
<jvmarg value="-Dproperty=value"/><jmeterarg value="-qextra.properties"/>
-->
<!-- Force suitable defaults -->
<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>
<property name="lib.dir" value="${jmeter.home}/apiTest/lib"/>
<!-- Use xalan copy from JMeter lib directory to ensure consistent processing with Java 1.4+ --> <path id="xslt.classpath">
<fileset dir="${lib.dir}" includes="xalan*.jar"/>
<fileset dir="${lib.dir}" includes="serializer*.jar"/> </path>
<!—测试报告 --> <target name="report" depends="xslt-report,copy-images">
<echo>Report generated at ${report.datestamp}</echo> </target>
<target name="xslt-report" depends="_message_xalan">
<tstamp><format property="report.datestamp" pattern="yyyy/MM/dd HH:mm"/></tstamp><xslt
classpathref="xslt.classpath"
force="true"
in="${testpath}/extras/${test}.jtl"
out="${testpath}/extras/${test}.html"
style="${basedir}/extras/jmeter-results-detail-report${style_version}.xsl">
<!—测试报告模板-->
<param name="showData" expression="${show-data}"/>
<param name="titleReport" expression="${report.title}"/>
<param name="dateReport" expression="${report.datestamp}"/>
</xslt>
</target>
<!-- Copy report images if needed -->
<target name="copy-images" depends="verify-images" unless="samepath">
<copy file="${basedir}/extras/expand.png" tofile="${testpath}/extras/expand.png"/>
<copy file="${basedir}/extras/collapse.png" tofile="${testpath}/extras/collapse.png"/> </target>
<target name="verify-images"> <condition property="samepath">
<equals arg1="${testpath}/extras" arg2="${basedir}/extras" /> </condition>
</target>
<!-- Check that the xalan libraries are present --> <condition property="xalan.present">
<and>
<!-- No need to check all jars; just check a few -->
<available classpathref="xslt.classpath"
classname="org.apache.xalan.processor.TransformerFactoryImpl"/>
<available
classpathref="xslt.classpath" classname="org.apache.xml.serializer.ExtendedContentHandler"/>
</and>
</condition>
<target name="_message_xalan" unless="xalan.present">
<echo>Cannot find all xalan and/or serialiser jars</echo>
<echo>The XSLT formatting may not work correctly.</echo>
<echo>Check you have xalan and serializer jars in ${lib.dir}</echo>
</target>
</project>