Jenkins搭建Ant+Jmeter持续测试平台

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

一、环境准备:

1、JDK:www.oracle.com/technetwork…

2、Jmeter:jmeter.apache.org/download_jm…

3、Ant:ant.apache.org/bindownload…

4、Jenkins:jenkins-ci.org/

java -jar jenkins.war --httpPort=8081 (8080是默认端口) C:\Users\用户名\.jenkins\ 默认安装根目录 
# 如果没有指定安装目录,那么它是根据JNENKINS_HOME环境变量来确定启动目录,没有才是默认目录
复制代码

二、Jemter脚本准备:

1、脚本目录:D:\JmeterTestScript\Script

image.png 2、脚本内容:业务接口调基础接口BasicPlatform.jmx

image.png

三、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="JmeterTest" default="run" basedir=".">
    
	    <tstamp>
        <format property="time" pattern="yyyyMMddhhmmss" />  <!--这里放置是time格式,方便下面报告名称引用${time},可以自定义时间显示格式,用-连接-->
        </tstamp>

        <!-- 修改成项目的目录-->
	    <property name="jmeter.home" value="${basedir}\target\jmeter" />
    	<property name="jmeter.home.bin" value="${jmeter.home}\bin" />
    	<property name="jmeter.lib" value="${basedir}\target\jmeter\lib" />
	    <property name="jmeter.report.dir" value="${basedir}\target\jmeter\dir" />
	    <property name="jmeter.report.jtl.dir" value="${basedir}\target\jmeter\jtl" />
	    <property name="jmeter.report.html.dir" value="${basedir}\target\jmeter\html" />
	    <property name="jmeter.report.results.dir" value="${basedir}\target\jmeter\results" />
    	<property name="jmeter.script.dir" value="${basedir}\target\jmeter\testFiles" />
    	
    	<property name="ReportName" value="TestReport"/>
    	<property name="jmeter.result.jtlName" value="${jmeter.report.jtl.dir}/${ReportName}${time}.jtl"/>
    	<property name="jmeter.result.htmlName" value="${jmeter.report.html.dir}/${ReportName}${time}.html"/>
    	<property name="ReportName" value="TestReport" />
    	
	    <target name="run">
	    	<antcall target="run_script" />
	    	<antcall target="report_detail" />
    	</target>
    	
    	<target name="run_script">
    	      <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
    		  <echo message="运行jmeter脚本jmx..."/>
    		  <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}">
    		  <!-- 声明要运行的脚本。"*.jmx"指包含此目录下的所有jmeter脚本 -->
    		  <testplans dir="${jmeter.script.dir}" includes="BasicPlatform.jmx" />
    		  <!--设置jmeter/bin目录下的jmeter.properties配置项,内容输出格式-->
    	      <property name="jmeter.save.saveservice.output_format" value="xml" />
    		  </jmeter>
    	</target>
    	
        <path id="xslt.classpath">
              <fileset dir="${jmeter.lib}" includes="xalan*.jar"/>
   	          <fileset dir="${jmeter.lib}" includes="serializer*.jar"/>
        </path>
    	
 	<target name="report_detail">
	        <tstamp> <format property="report.datestamp" pattern="yyyy/MM/dd HH:mm" /></tstamp>
		<echo message="接口测试详细报告"/>
		<echo message="     "/>
		<xslt 
                classpathref="xslt.classpath"
                force="true"
		in="${jmeter.result.jtlName}" 
		out="${jmeter.result.htmlName}" 
                style="${jmeter.home}/resources/jmeter.results.shanhe.me.xsl">
                <param name="Report_detail_time" expression="${report.datestamp}"/>
		</xslt>
        <!--
        <copy todir="${jmeter.result.dir}">
                <fileset dir="${jmeter.home}/resources">
                <include name="collapse.png" />
                <include name="expand.png" />
                </fileset>
       </copy>
	   -->
       </target>
    </project>
复制代码

四、配置Jenkins Job并运行:

1、job配置如下:

image.png 2、job运行结果:

Started by user 超级管理员
Building in workspace C:\Users\Joe\.jenkins\workspace\BasicPlatForm
[jmeter_ant] $ cmd.exe /C "ant.bat -file eclipse_jmeter_ant.xml run && exit %%ERRORLEVEL%%"
Buildfile: C:\Users\Administrator\workspace\jmeter_ant\eclipse_jmeter_ant.xml

run:

run_script:
     [echo] 运行jmeter脚本jmx...
   [jmeter] Executing test plan: C:\Users\Administrator\workspace\jmeter_ant\target\jmeter\testFiles\BasicPlatform.jmx ==> C:\Users\Administrator\workspace\jmeter_ant\target\jmeter\jtl\TestReport20180627101425.jtl
   [jmeter] Creating summariser <summary>
   [jmeter] Created the tree successfully using C:\Users\Administrator\workspace\jmeter_ant\target\jmeter\testFiles\BasicPlatform.jmx
   [jmeter] Starting the test @ Wed Jun 27 22:14:41 CST 2018 (1530108881101)
   [jmeter] Waiting for possible Shutdown/StopTestNow/Heapdump message on port 4445
   [jmeter] summary +      3 in 00:00:18 =    0.2/s Avg:  3056 Min:   381 Max:  5748 Err:     0 (0.00%) Active: 1 Started: 1 Finished: 0
   [jmeter] 
   [jmeter] 
   [jmeter] 加密接连串currPage=1&pageSize=20&partnerId=QGZ&userId=740bc586-102c-49a8-9710-8969ea1da856qVJIj7pAeUnb0xatC9VD98UqzxMPxm4N
   [jmeter] 2314aeaa2f3c58b254ee83f835114082912da296
   [jmeter] summary +      3 in 00:00:07 =    0.4/s Avg:   854 Min:   254 Max:  2025 Err:     0 (0.00%) Active: 0 Started: 1 Finished: 1
   [jmeter] summary =      6 in 00:00:25 =    0.2/s Avg:  1955 Min:   254 Max:  5748 Err:     0 (0.00%)
   [jmeter] Tidying up ...    @ Wed Jun 27 22:15:07 CST 2018 (1530108907952)
   [jmeter] ... end of run

report_detail:
     [echo] 接口测试详细报告
     [echo]      
     [xslt] Processing C:\Users\Administrator\workspace\jmeter_ant\target\jmeter\jtl\TestReport20180627101425.jtl to C:\Users\Administrator\workspace\jmeter_ant\target\jmeter\html\TestReport20180627101425.html
     [xslt] Loading stylesheet C:\Users\Administrator\workspace\jmeter_ant\target\jmeter\resources\jmeter.results.shanhe.me.xsl
BUILD SUCCESSFUL
Total time: 45 seconds
Finished: SUCCESS
复制代码

3、在job的workspace目录下会生成结果报告:

image.png

4、TestReport.html:

image.png

更新补充:

  1、Date report这里的时间没有正确显示出来

  2、Summary里的字段Min Time和Max Time显示的是NaN,没有显示正确的时间。

五、配置发送邮件功能

 1、自已写一个发送邮件的功能并打成sendmail.jar包,放在job的workspace目录中

image.png

2、jenkins增加构建步骤

  a)进入到测试报告的目录

  b) 调用sendmail.jar命令发送邮件

image.png

说明:

  1、由build3.xml的第12、13行可知,报告文件生成目录为: e n v . W O R K S P A C E / r e s u l t s / {env.WORKSPACE}/results/ {env.BUILD_ID},所以这里我要先cd到具体执行的那个build_id目录下。

   2、我们也可以把上面的两行命令写在成一个批处理文件,例如我第1步有个sendmail.bat文件就是的,然后调用时直接写sendmail.bat就好了.

猜你喜欢

转载自juejin.im/post/7019695761671585800