jmeter+ant+jenkins-接口自动化持续集成
- jmeter+ant+jenkins-接口自动化持续集成
- 基本安装配置
- Ant配置Jmeter
- 配置Jenkins,构建持续集成
jmeter+ant+jenkins-接口自动化持续集成
Jmeter可以做接口测试,也能做压力测试,而且是开源软件
Ant是基于Java的构建工具,完成脚本执行并收集结果生成报告,可以跨平台
Jenkins是持续集成工具
基本安装配置
- 安装jmeter
环境变量配置:
JMETER_HOME 为 jemter安装路径;
CLASSPATH为 %JMETER_HOME%\lib;
PATH为%JMETER_HOME%\bin; - 安装jdk
环境变量配置:
JAVA_HOME 为 C:\Program Files (x86)\Java\jdk1.8.0_171 (注意:java我是默认装的C盘)
CLASSPATH为 .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar;
PATH为 %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; - 安装ant
下载地址:https://ant.apache.org/manualdownload.cgi 下载后与jmeter放置同一位置
环境变量配置:
ANT_HOME 为 ant解压位置
CLASSPATH为 %ANT_HOME%\lib;
PATH为%ANT_HOME%\bin;配置环境变量时后面不加;
验证是否安装成功
Ant配置Jmeter
用ant构建命令调动执行jmeter接口测试,并生成测试报告
4. 配置库文件
将jmeter extras目录下的ant-jmeter-1.1.1.jar文件拷贝到ant安装目录下的lib文件夹中
- 配置ant的编译文件build.xml
<?xml version="1.0"?>
<!--
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>
<tstamp>
<format property="time" pattern="yyyyMMddhhmm" />
</tstamp>
<property name="testpath" value="D:\Jenkins\workspace\autotest-operatormanagement"/>
<property name="jmeter.home" value="D:\jmeter"/>
<property name="report.title" value="hcharger接口测试报告"/>
<property name="ReportName" value="TestReport" />
<property name="test" value="account"/>
<property name="show-data" value="y"/>
<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="n"/>
</condition>
<condition property="funcMode" value="true">
<not>
<equals arg1="${show-data}" arg2="y"/>
</not>
</condition>
<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>
<delete file="${testpath}/report/${ReportName}${time}.html"/>
<jmeter jmeterhome="${jmeter.home}" resultlog="${testpath}/result/${ReportName}${time}.jtl">
<testplans dir="${testpath}/jmx" includes="*.jmx" />
<jvmarg value="-Xincgc"/>
<jvmarg value="-Xmx128m"/>
<jvmarg value="-Dproperty=value"/>
<jmeterarg value="-qextra.properties"/>
<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"/>
<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="result/${ReportName}${time}.jtl"
out="report/${ReportName}${time}.html"
style="${jmeter.home}/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>
<target name="copy-images" depends="verify-images" unless="samepath">
<copy file="${jmeter.home}/extras/expand.png" tofile="${testpath}/report/expand.png"/>
<copy file="${jmeter.home}/extras/collapse.png" tofile="${testpath}/report/collapse.png"/>
</target>
<target name="verify-images">
<condition property="samepath">
<equals arg1="${testpath}" arg2="${basedir}" />
</condition>
</target>
<condition property="xalan.present">
<and>
<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>
- 配置jmeter.propertise文档
找到jmeter.properties文档,在jmeter/bin目录下,打开该文档并编辑,修改jmeter报告输出格式为xml:
改jmeter.save.saveservice.output_format=csv 为jmeter.save.saveservice.output_format=xml,并去掉前面的注释符号#
- 验证配置并构建测试
在jmeter-bin下新建anttest文件夹,在anttest文件夹下新建jmx文件夹,并将.jmx脚本放进去
cmd在cd到build.xml文件所在目录,输入ant回车,执行测试
执行结果如下: - 查看测试报告
- 这样的结果是不是不太直观,因为用jmeter自带的测试报告得到的测试报告信息并不是很全,需要完善测试报告
- 完善测试报告
下载优化模板jmeter-results-shanhe-me.xsl,拷贝到jmeter的extras目录中
设置测试输出报告要输出的内容:同样在jmeter.properties中,设置需要输出的内容为true,并去掉前面的注释符号#,这里全部设置成true→保存 - 设置build文件的报告模板为优化后的模板jmeter-results-shanhe-me.xsl
- 重新ant构建测试,查看测试报告(不同模板,生成的测试报告不同)
配置Jenkins,构建持续集成
下载安装包(安装过程略)
配置环境变量
JENKINS_HOME 为 C:\Program Files (x86)\Jenkins
启动Jenkins
浏览器中输入远程服务器配置的地址,用户名及密码,登录jenkins
打开jenkins,配置用户名、密码及插件,
这里需要配置invoke ant插件、HTML测试报告展示的插件
new item