前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Jmeter篇】jmeter+Ant+Jenkins接口自动化测试集成(一)

【Jmeter篇】jmeter+Ant+Jenkins接口自动化测试集成(一)

作者头像
王大力测试进阶之路
发布2019-10-25 18:09:32
4.8K0
发布2019-10-25 18:09:32
举报
文章被收录于专栏:橙子探索测试橙子探索测试

一.简介

1、什么是ant?

ant是构建工具,把代码从某个地方拿来,编译,再拷贝到某个地方去等等操作

JMeter+Ant是比较常见的自动化测试框架,因为JMeter、Ant都是由java开发的,所以此性能测试框架具有良好的跨平台性;下图是按自己的理解绘制的自动化框架图:

2、ant的好处

跨平台 --因为ant是使用java实现的,所以它跨平台

使用简单--与ant的兄弟make比起来

语法清晰--同样是和make相比

功能强大--ant能做的事情很多,可能你用了很久,你仍然不知道它能有多少功能。当你自己开发一些ant插件的时候,你会发现它更多的功能。

3、实现过程

(1)Ant通过XML文件进行构建,所有的构建信息配置在build.xml文件中,通过调用target树来执行各种任务;如执行测试脚本(jmx文件),输出测试结果(jtl文件);

(2)Ant通过测试结果(jtl文件)构建生成html测试报告,输出在制定路径下;

(3)根据build.xml文件中配置的邮件信息,将html测试报告自动发送到制定的邮箱;

4、原理

jenkins驱动ant执行,ant驱动jmeter执行

二.配置ant环境

1、去Apache官网上http://ant.apache.org/bindownload.cgi下载ant,我下载的是apach-ant-1.10.7

2、下完之后解压,放到一个目录下面。我把它和jmeter都放在了一个目录下C:\jmeter\apache-ant-1.10.7

3、配置环境变量,与jmeter类似。

新建系统变量ANT_HOME,值C:\jmeter\apache-ant-1.10.7

系统变量CLASSPATH,值后面添加C:\jmeter\apache-ant-1.10.7\lib

系统变量Path,值后面添加C:\jmeter\apache-ant-1.10.7\bin

cmd下,输入ant,结果如下图所示,"build.xml does not exist "说明ant配置成功

三、Jmeter与Ant集成使用

准备环境:jdk1.8.0_60,环境变量的配置;jmeter5.0安装,环境变量的配置;ant1.10.7的安装,环境变量的配置

1、将C:\jmeter\apache-jmeter-5.0\extras文件夹下的ant-jmeter-1.1.1.jar这个jar包复制到C:\jmeter\apache-ant-1.10.7\lib文件夹下

2、打开C:\jmeter\apache-jmeter-5.0\bin\jmeter.properties 将jmeter.save.saveservice.output_format=csv 改成 jmeter.save.saveservice.output_format=xml,记得去掉前面的“#”

3、.jmx脚本放置C:\jmeter\apache-jmeter-5.0\extras下

4、进入C:\jmeter\apache-jmeter-5.0\extras,打开build.xml文件,进行配置

第一处要修改的地方

<!-- Name of test (without .jmx) -->

<property name="test1" value="insight2.0-180720"/>

此处填写你要测试的脚本,后面不用写.jmx,只用写文件名。这里的意思为test1的值为insight2.0-180720,你将要测试的脚本就是insight2.0-180720.jmx

当然property name的也可以取其他的,test、a、b.....等等都可以,简单理解为一个变量名,value就是变量值。

第二处要修改的地方

在<target name = "run"> 下面修改子标签jmeter

<jmeter

jmeterhome="${jmeter.home}"

testplan ="${testpath}/${test1}.jmx"

resultlog="${testpath}/${test1}.jtl">

........

</jmeter>

这里就开始运行jmx脚本了,测试计划testplan的值就是上一处你定义的test1;resultlog就是执行jmx之后生成的日志文件,它会默认保存在extras文件夹下。

第三处要修改的地方

在<target name="xslt-report" depends="_message_xalan">下面修改子标签xslt

<xslt

classpathref="xslt.classpath"

force="true"

in="${testpath}/${test1}.jtl"

out="${testpath}/${test1}.html"

........

</xslt>

这里做的工作是,将jmeter执行之后生成的jtl文件,编译构建成HTML文件,默认保存在extras文件夹下。

仔细看,in等于的就是上一处的rasultlog,out的后缀为html

第四处要修改的地方

我想每执行一次脚本,就生成一个HTML、一个jtl文件,并且文件名能显示每次执行的时间。这里可以联想到时间戳,每次运行结果都生成不一样的文件。

1、定义指定格式的时间变量。可以直接在target name =run下定义

<tstamp>

<format property="time" pattern="yyyy-MM-dd-HH-mm-ss"/>

</tstamp>

2、在jmeter标签下,修改jtl文件的存储路径

<jmeter

jmeterhome="${jmeter.home}"

testplan ="${testpath}/${test1}.jmx"

resultlog="${testpath}/${test1}/${time}.jtl">

...</jmeter>

3、在xslt标签下,修改in、out的路径

<xslt

classpathref="xslt.classpath"

force="true"

in="${testpath}/${test1}/${time}.jtl"

out="${testpath}/${test1}/${time}.html"

.....</xslt>

第五处要修改的地方

build.xml文件无法使用*.jmx执行extras目录下所有的jmx文件,那么如果想每一次想执行多个脚本怎么办呢??目前想到了一个解决办法。大概思路就是,定义多个变量,分别执行。

<!-- Name of test (without .jmx) -->

<property name="test1" value="要执行的脚本1"/>

<property name="test2" value="要执行的脚本2"/>

<property name="test3" value="要执行的脚本3"/>

.........

5、修改完build.xml文件之后,就可以试一下jmeter+ant的集成有没有成功,打开命令提示符,进入到C:\jmeter\apache-jmeter-5.0\extras,在这里输入ant。

最后面显示build successful说明执行成功了!可以进入到extras文件夹下面查看结果,生成了HTML和jtl文件

6、打开html测试报告如下

7、聚合报告打开jtl文件如下

8、build.xml内容如下

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
    
       http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<project name="ant-jmeter" default="all">
    <description>
 
        Sample build file for use with ant-jmeter.jar
        See http://www.programmerplanet.org/pages/projects/jmeter-ant-task.php
 
    To run a test and create the output report:
        ant -Dtest=script
 
    To run a test only:
        ant -Dtest=script run
 
    To run report on existing test output
        ant -Dtest=script report
 
    The "script" parameter is the name of the script without the .jmx suffix.
 
    Additional options:
        -Dshow-data=y - include response data in Failure Details
        -Dtestpath=xyz - path to test file(s) (default user.dir).
                         N.B. Ant interprets relative paths against the build file
        -Djmeter.home=.. - path to JMeter home directory (defaults to parent of this build file)
        -Dreport.title="My Report" - title for html report (default is 'Load Test Results')
    </description>
    
    <property name="testpath" value="${user.dir}"/>
    <property name="jmeter.home" value="${basedir}/.."/>
    <property name="report.title" value="Load Test Results"/>
    
    <!-- Name of test (without .jmx) -->
    <property name="test1" value="insight2.0-180720"/>
    
    <!-- Should report include response data for failures? -->
    <property name="show-data" value="n"/>
 
    <property name="format" value="2.1"/>
    
    <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}">
          <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>
    
    <tstamp>
      <format property="time" pattern="yyyy-MM-dd-HH-mm-ss"/>
      </tstamp>
      
        <delete file="${testpath}/${test}.html"/>
        <jmeter
            jmeterhome="${jmeter.home}"
            testplan ="${testpath}/${test1}.jmx"
            resultlog="${testpath}/${test1}/${time}.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}/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}/${test1}/${time}.jtl"
            out="${testpath}/${test1}/${time}.html"
            style="${basedir}/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}/expand.png" tofile="${testpath}/expand.png"/>
        <copy file="${basedir}/collapse.png" tofile="${testpath}/collapse.png"/>
    </target>
 
    <target name="verify-images">
        <condition property="samepath">
                <equals arg1="${testpath}" arg2="${basedir}" />
        </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>
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-10-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 橙子探索测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档