其他分享
首页 > 其他分享> > Jmeter+ant+Jenkins构建接口自动化测试

Jmeter+ant+Jenkins构建接口自动化测试

作者:互联网

1、已写好jmeter脚本
2、安装ant并将ant-jmeter-1.1.1.jar文件放入ant/lib目录,用于调用jmeter
3、修改jmeter的jmeter.properties文件(将下面配置直接放入改文件):

            jmeter.save.saveservice.output_format=xml 
            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=true
            jmeter.save.saveservice.requestHeaders=true
            jmeter.save.saveservice.encoding=false
            jmeter.save.saveservice.bytes=true
            jmeter.save.saveservice.url=true
            jmeter.save.saveservice.filename=true
            jmeter.save.saveservice.hostname=true
            jmeter.save.saveservice.thread_counts=true
            jmeter.save.saveservice.sample_count=true
            jmeter.save.saveservice.idle_time=true

4、网上找个jmeter.results.shanhe.me.new.xsl包放入jmeter\extras目录中:报告输出模板

5、新建一个TestCase(用于放测试脚本及报告和构建文件build.xml)

6、在TestCase新建report文件和build.xml文件,及将执行脚本放入TestCase文件中:
report文件新建HTML和JTL文件夹用于存放生成的html报告及jtl文件
build.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project name="ant-jmeter-test" default="run" basedir=".">
<!-- 需要改成自己本地的 Jmeter 目录-->  
<property name="jmeter.home" value="E:\apache-jmeter-5.0\apache-jmeter-5.0" />
<property name="report.title" value="接口测试"/>
<!-- jmeter生成jtl格式的结果报告的路径--> 
<property name="jmeter.result.jtl.dir" value="E:\apache-jmeter-5.0\apache-jmeter-5.0\TestCase\report\JTL" />
<!-- jmeter生成html格式的结果报告的路径-->
<property name="jmeter.result.html.dir" value="E:\apache-jmeter-5.0\apache-jmeter-5.0\TestCase\report\HTML" />
<!-- 生成的报告的前缀-->  
<property name="ReportName" value="TestReport" />
<property name="jmeter.result.jtlName" value="${jmeter.result.jtl.dir}/${ReportName}.jtl" />
<property name="jmeter.result.htmlName" value="${jmeter.result.html.dir}/${ReportName}.html" />
<target name="run">
    <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脚本-->
        <testplans dir="E:\apache-jmeter-5.0\apache-jmeter-5.0\TestCase" includes="*.jmx" />
        <property name="jmeter.save.saveservice.output_format" value="xml"/>
    </jmeter>
</target>
<path id="xslt.classpath">
    <fileset dir="${jmeter.home}/lib" includes="xalan*.jar"/>
    <fileset dir="${jmeter.home}/lib" includes="serializer*.jar"/>
</path>
<target name="report">
    <tstamp> <format property="report.datestamp" pattern="yyyy/MM/dd HH:mm" /></tstamp>
    <xslt 
          classpathref="xslt.classpath"
          force="true"
          in="${jmeter.result.jtlName}"
          out="${jmeter.result.htmlName}"                  style="${jmeter.home}/extras/jmeter.results.shanhe.me.new.xsl">
          <param name="dateReport" expression="${report.datestamp}"/>
   </xslt>

            <!-- 因为上面生成报告的时候,不会将相关的图片也一起拷贝至目标目录,所以,需要手动拷贝 --> 
    <copy todir="${jmeter.result.html.dir}">
        <fileset dir="${jmeter.home}/extras">
            <include name="collapse.png" />
            <include name="expand.png" />
        </fileset>
    </copy>
</target>

标签:save,jmeter,文件,ant,Jenkins,Jmeter,true,response,saveservice
来源: https://www.cnblogs.com/CesareZhang/p/10772833.html