Ant+Jmeter自动化接口测试

Ant驱动Jmeter
单独使用Jmeter来执行接口测试是非常简单的了,使用Ant来驱动Jmeter就需要些一个构建文件build.xml


<project name="Ant-Jmeter-Test" default="all" basedir=".">
    <tstamp>
        <format property="time" pattern="yyyyMMddhhmm" />
    tstamp>
    
    <property name="jmeter.home" value="/Users/gengmei/Downloads/apache-jmeter-3.0" />
    
    <property name="jmeter.result.jtl.dir" value="/Users/gengmei/Downloads/jmeterTest/resultLog/jtl" />
    
    <property name="jmeter.result.html.dir" value="/Users/gengmei/Downloads/jmeterTest/resultLog/html" />
    
    <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" />
    
    <property name="mail_to" value="[email protected]" />
    
    <property name="ComputerName" value="172.30.9.158" />
    <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}">
            
            <testplans dir="/Users/gengmei/Downloads/jmeterTest/script" includes="*.jmx" />
        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.html.dir}">
            <fileset dir="${jmeter.home}/extras">
                <include name="collapse.png" />
                <include name="expand.png" />
            fileset>
        copy>
    target>
    
project>

只要Ant配置好,直接运行Ant就行了。结果如下:

SvenWeng@wengyanbin:~/Desktop|⇒  ant
Buildfile: /Users/SvenWeng/Desktop/build.xml

all:

test:
   [jmeter] Executing test plan: /Users/SvenWeng/Desktop/获取用户ID.jmx ==> /Users/SvenWeng/Desktop/TestReport201608220301.jtl
   [jmeter] Writing log file to: /Users/SvenWeng/apache-jmeter-3.0/bin/jmeter.log
   [jmeter] Creating summariser 
   [jmeter] Created the tree successfully using /Users/SvenWeng/Desktop/获取用户ID.jmx
   [jmeter] Starting the test @ Mon Aug 22 15:01:29 CST 2016 (1471849289646)
   [jmeter] Waiting for possible Shutdown/StopTestNow/Heapdump message on port 4445
   [jmeter] summary +      1 in 00:00:01 =    2.0/s Avg:   267 Min:   267 Max:   267 Err:     0 (0.00%) Active: 37 Started: 38 Finished: 1
   [jmeter] summary +   3451 in 00:00:30 =  115.9/s Avg:   841 Min:   130 Max:  3745 Err:     0 (0.00%) Active: 100 Started: 101 Finished: 1
   [jmeter] summary =   3452 in 00:00:30 =  114.0/s Avg:   841 Min:   130 Max:  3745 Err:     0 (0.00%)
   [jmeter] summary +   1548 in 00:00:14 =  107.7/s Avg:   792 Min:    32 Max:  5003 Err:     0 (0.00%) Active: 0 Started: 101 Finished: 101
   [jmeter] summary =   5000 in 00:00:45 =  111.9/s Avg:   826 Min:    32 Max:  5003 Err:     0 (0.00%)
   [jmeter] Tidying up ...    @ Mon Aug 22 15:02:14 CST 2016 (1471849334383)
   [jmeter] ... end of run

report:
     [xslt] Processing /Users/SvenWeng/Desktop/TestReport201608220301.jtl to /Users/SvenWeng/Desktop/TestReport201608220301.html
     [xslt] Loading stylesheet /Users/SvenWeng/apache-jmeter-3.0/extras/jmeter-results-detail-report_21.xsl
     [copy] Copying 2 files to /Users/SvenWeng/Desktop

BUILD SUCCESSFUL
Total time: 47 seconds

几个大坑

taskdef class org.programmerplanet.ant.taskdefs.jmeter.JMeterTask cannot be found
using the classloader AntClassLoader[]

这个报错非常坑爹,是由于Ant有一个ant-jmeter-1.1.1.jar这个文件缺失了,所以一直会报这个错。

stylesheet /Users/SvenWeng/apache-jmeter-3.0/extras/jmeter-results-detail-report_21.xsl doesn't exist.
这个报错是由于我使用的是Jmeter3.0。而3.0文件下面的对应文件是jmeter-results-detail-report.xsl 所以报了这个错,但是这个文件是有问题的.

解决办法:
1,下载Jmeter2.*的文件包
2,找到红框中的文件复制到jmeter3.0对应的文件夹中Ant+Jmeter自动化接口测试_第1张图片
测试报告中三个指标为NaN
这个问题也是一个坑,我找了好久才找到原因。

需要从Jmeter的lib包里把xalan-2.7.2.jar和serializer-2.7.2.jar copy到Ant的lib包里。

你可能感兴趣的:(python自动化)