JMeter+ant+Jenkins接口自动化测试

参考地址1:https://www.cnblogs.com/wulixia/p/11011793.html

参考地址2:https://www.cnblogs.com/lxs1314/p/7487066.html

1.安装JDK

2.安装JMeter

3.安装ANT

4.拷贝JMeter目录D:\Software\apache-jmeter-5.3\extras下ant-jmeter-1.1.1.jar文件到ant的lib目录下

5.修改Bulid.xml

6.修改jmeter.properties中的配置文件

7.修改报告模板jmeter-results-report_21.xsl

8.安装Jenkins

BUILD.XML代码:

<?xml version="1.0" encoding="UTF-8"?>

<project name="ant-jmeter-test" default="run" basedir=".">
    <tstamp>
        <format property="time" pattern="yyyyMMddhhmm" />
		<format property="time1" pattern="yyyy-MM-dd" />
    </tstamp>
    <!-- 需要改成自己本地的 Jmeter 目录-->  
    <property name="jmeter.home" value="D:\Software\apache-jmeter-5.3" />
    <!-- jmeter生成jtl格式的结果报告的路径--> 
    <property name="jmeter.result.jtl.dir" value="D:\Software\apache-jmeter-5.3\report" />
    <!-- jmeter生成html格式的结果报告的路径-->
    <property name="jmeter.result.html.dir" value="D:\Software\apache-jmeter-5.3\report" />
    <!-- 生成的报告的前缀-->  
    <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}_Summary.html" />
	<property name="jmeter.result.htmlName1" value="${jmeter.result.html.dir}/${ReportName}${time}_Details.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="D:\Software\apache-jmeter-5.3\Script" 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"/>
		<format property="report.dateReport1" pattern="yyyy-MM-dd"/>
		</tstamp>
        <xslt 
              classpathref="xslt.classpath"
              force="true"
              in="${jmeter.result.jtlName}"
              out="${jmeter.result.htmlName}"
              style="${jmeter.home}/extras/jmeter-results-report_21.xsl">
              <param name="dateReport" expression="${report.datestamp}"/>
			  <param name="dateReport" expression="${report.dateReport1}"/>
        </xslt>
		
		<xslt 
              classpathref="xslt.classpath"
              force="true"
              in="${jmeter.result.jtlName}"
              out="${jmeter.result.htmlName1}"
              style="${jmeter.home}/extras/jmeter.results.shanhe.me.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>
</project>

修改jmeter-results-report_21.xsl模板

<?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.
  -->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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.
-->

<!--
	Stylesheet for processing 2.1 output format test result files
	To uses this directly in a browser, add the following to the JTL file as line 2:
	<? xml-stylesheet type="text/xsl" href="../extras/jmeter-results-report_21.xsl" ?>
	and you can then view the JTL in a browser
-->

<xsl:output method="html" indent="yes" encoding="UTF-8" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" />

<xsl:param name="titleReport" select="'Load Test Results'"/>
<xsl:param name="dateReport" select="'date not defined'"/>
<xsl:param name="dateReport1" select="'date not defined'"/>

<xsl:template match="testResults">
	<html>
		<head>
			<title><xsl:value-of select="$titleReport" /></title>
			<style type="text/css">
				body {
					font:normal 80% 微软雅黑;
					color:#000000;
				}
				table tr td, table tr th {
					font-size: 95%;
					font-family:微软雅黑;
				}
				table.details tr th{
				    color: #ffffff;
					font-weight: bold;
					text-align:center;
					background:#2674a6;
					white-space: nowrap;
				}
				table.details tr td{
					background:#eeeee0;
					white-space: nowrap;
				}
				h1 {
					margin: 0px 0px 5px; font: 165% verdana,arial,helvetica
				}
				h2 {
					margin-top: 1em; margin-bottom: 0.5em; font: bold 125% verdana,arial,helvetica
				}
				h3 {
					margin-bottom: 0.5em; font: bold 115% verdana,arial,helvetica
				}
				.Failure {
					font-weight:bold; color:red;
				}
			</style>
		</head>
		<body>
		
			<xsl:call-template name="summary" />
			<!--hr size="1" width="95%" align="center" /-->
			
			<xsl:call-template name="pageHeader" />

			<xsl:call-template name="pagelist" />
			<!--hr size="1" width="95%" align="center" /-->

			<!--xsl:call-template name="detail" /-->

		</body>
	</html>
</xsl:template>

<xsl:template name="summary">
	
	<table id="a3" align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="80%">
	<tr valign="top" > 
	<th colspan="5" style="background-color:rgb(83,141,213);font-size:26px;font-family:微软雅黑">测试 <span style="color:rgb(255,192,0)">报告</span> API测试日报</th>
	</tr>
		<tr valign="top">
			<th>场景数</th>
			<th>用例总数</th>
			<th>通过总数</th>
			<th>失败总数</th>
			<th>通过率</th>
		</tr>
		<tr valign="top">
			<xsl:variable name="allCount" select="count(/testResults/*)" />
			<xsl:variable name="allFailureCount" select="count(/testResults/*[attribute::s='false'])" />
			<xsl:variable name="allSuccessCount" select="count(/testResults/*[attribute::s='true'])" />
			<xsl:variable name="allSuccessPercent" select="$allSuccessCount div $allCount" />
			<xsl:variable name="allTotalTime" select="sum(/testResults/*/@t)" />
			<xsl:variable name="allAverageTime" select="$allTotalTime div $allCount" />
			<xsl:variable name="allMinTime">
				<xsl:call-template name="min">
					<xsl:with-param name="nodes" select="/testResults/*/@t" />
				</xsl:call-template>
			</xsl:variable>
			<xsl:variable name="allMaxTime">
				<xsl:call-template name="max">
					<xsl:with-param name="nodes" select="/testResults/*/@t" />
				</xsl:call-template>
			</xsl:variable>
			<!--xsl:attribute name="class">
				<xsl:choose>
					<xsl:when test="$allFailureCount &gt; 0">Failure</xsl:when>
				</xsl:choose>
			</xsl:attribute-->
			<td align="center" id="CJ_Count">
				1
			</td>
			<td align="center">
				<xsl:value-of select="$allCount" />
			</td>
			<td align="center">
				<xsl:value-of select="$allSuccessCount" />
			</td>
			<td align="center">
				<xsl:attribute name="class">
					<xsl:choose>
						<xsl:when test="$allFailureCount > 0">Failure</xsl:when>
					</xsl:choose>
				</xsl:attribute>
				<xsl:value-of select="$allFailureCount" />
			</td>
			<td align="center">
				<xsl:attribute name="class">
					<xsl:choose>
						<xsl:when test="$allFailureCount > 0">Failure</xsl:when>
					</xsl:choose>
				</xsl:attribute>
				<xsl:call-template name="display-percent">
					<xsl:with-param name="value" select="$allSuccessPercent" />
				</xsl:call-template>
			</td>
			<!--td align="right">
				<xsl:call-template name="display-time">
					<xsl:with-param name="value" select="$allAverageTime" />
				</xsl:call-template>
			</td-->
			<!--td align="right">
				<xsl:call-template name="display-time">
					<xsl:with-param name="value" select="$allMinTime" />
				</xsl:call-template>
			</td-->
			<!--td align="right">
				<xsl:call-template name="display-time">
					<xsl:with-param name="value" select="$allMaxTime" />
				</xsl:call-template>
			</td-->
		</tr>
	</table>
</xsl:template>

<xsl:template name="pageHeader">
	<!--h1><xsl:value-of select="$titleReport" /></h1-->
	 <table id="a1" align="center" width="80%"> 
	 <tr> 
	 <td align="left" style="font-weight: bold;"><i>用例执行明细</i></td>
	 <td align="right"><a href="http://192.168.1.1:8080/jenkins/job/JMeter_API/HTML_20Report/TestReport-B{$dateReport1}.html"><i>详情请点击查看</i></a> </td> 
	 </tr> 
	 </table>
</xsl:template>


<xsl:template name="pagelist">
	
	<table id="a4" align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="80%">
		<tr valign="top">
			<th>场景名称</th>
			<th>用例数</th>
			<th>失败数</th>
			<th>通过率</th>
			<th>测试责任人</th>
			<!--th>Min Time</th-->
			<!--th>Max Time</th-->
		</tr>
		
			<xsl:variable name="label" select="/testResults/*[starts-with(@tn,'UI_')]" />
			<xsl:variable name="count" select="count(/testResults/*[starts-with(@tn,'UI_')])" />
			<xsl:variable name="failureCount" select="count(/testResults/*[starts-with(@tn,'UI_')][attribute::s='false'])" />
			<xsl:variable name="successCount" select="count(/testResults/*[starts-with(@tn,'UI_')][attribute::s='true'])" />
			<xsl:variable name="successPercent" select="$successCount div $count" />
			<xsl:variable name="totalTime" select="sum(/testResults/*[starts-with(@tn,'UI_')]/@t)" />
			<xsl:variable name="averageTime" select="$totalTime div $count" />
			<xsl:variable name="minTime">
				<xsl:call-template name="min">
					<xsl:with-param name="nodes" select="/testResults/*[starts-with(@tn,'UI_')]/@t" />
				</xsl:call-template>
			</xsl:variable>
			<xsl:variable name="maxTime">
				<xsl:call-template name="max">
					<xsl:with-param name="nodes" select="/testResults/*[starts-with(@tn,'UI_')]/@t" />
				</xsl:call-template>
			</xsl:variable>
			<tr valign="top">
				<!--xsl:attribute name="class">
					<xsl:choose>
						<xsl:when test="$failureCount &gt; 0">Failure</xsl:when>
					</xsl:choose>
				</xsl:attribute-->
				<td align="center">
					UI模块
				</td>
				<!--td>
					<xsl:value-of select="$label" />
				</td-->
				<td align="center">
					<xsl:value-of select="$count" />
				</td>
				<td align="center">
					<xsl:attribute name="class">
					<xsl:choose>
						<xsl:when test="$failureCount > 0">Failure</xsl:when>
					</xsl:choose>
					</xsl:attribute>
					<xsl:value-of select="$failureCount" />
				</td>
				<td align="center">
					<xsl:attribute name="class">
					<xsl:choose>
						<xsl:when test="$failureCount > 0">Failure</xsl:when>
					</xsl:choose>
					</xsl:attribute>
					<xsl:call-template name="display-percent">
						<xsl:with-param name="value" select="$successPercent" />
					</xsl:call-template>
				</td>
				<td align="center">
					JDL
				</td>
				<!--td align="right">
					<xsl:call-template name="display-time">
						<xsl:with-param name="value" select="$averageTime" />
					</xsl:call-template>
				</td-->
				<!--td align="right">
					<xsl:call-template name="display-time">
						<xsl:with-param name="value" select="$minTime" />
					</xsl:call-template>
				</td-->
				<!--td align="right">
					<xsl:call-template name="display-time">
						<xsl:with-param name="value" select="$maxTime" />
					</xsl:call-template>
				</td-->
			</tr>
	</table>
</xsl:template>

<!--xsl:template name="detail">
	<xsl:variable name="allFailureCount" select="count(/testResults/*[attribute::s='false'])" />

	<xsl:if test="$allFailureCount > 0">
		<h2>Failure Detail</h2>

		<xsl:for-each select="/testResults/*[not(@lb = preceding::*/@lb)]">

			<xsl:variable name="failureCount" select="count(../*[@lb = current()/@lb][attribute::s='false'])" />

			<xsl:if test="$failureCount > 0">
				<h3><xsl:value-of select="@lb" /></h3>

				<table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
				<tr valign="top">
					<th>Response</th>
					<th>Failure Message</th>
				</tr>

				<xsl:for-each select="/testResults/*[@lb = current()/@lb][attribute::s='false']">
					<tr>
						<td><xsl:value-of select="@rc | @rs" /> - <xsl:value-of select="@rm" /></td>
						<td><xsl:value-of select="assertionResult/failureMessage" /></td>
					</tr>
				</xsl:for-each>

				</table>
			</xsl:if>

		</xsl:for-each>
	</xsl:if>
</xsl:template-->

<xsl:template name="min">
	<xsl:param name="nodes" select="/.." />
	<xsl:choose>
		<xsl:when test="not($nodes)">NaN</xsl:when>
		<xsl:otherwise>
			<xsl:for-each select="$nodes">
				<xsl:sort data-type="number" />
				<xsl:if test="position() = 1">
					<xsl:value-of select="number(.)" />
				</xsl:if>
			</xsl:for-each>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="max">
	<xsl:param name="nodes" select="/.." />
	<xsl:choose>
		<xsl:when test="not($nodes)">NaN</xsl:when>
		<xsl:otherwise>
			<xsl:for-each select="$nodes">
				<xsl:sort data-type="number" order="descending" />
				<xsl:if test="position() = 1">
					<xsl:value-of select="number(.)" />
				</xsl:if>
			</xsl:for-each>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="display-percent">
	<xsl:param name="value" />
	<xsl:value-of select="format-number($value,'0.00%')" />
</xsl:template>

<xsl:template name="display-time">
	<xsl:param name="value" />
	<xsl:value-of select="format-number($value,'0 ms')" />
</xsl:template>

</xsl:stylesheet>

问题一:发送HTML

邮件乱码

解决方案:

1、添加系统变量。
变量名:JAVA_TOOL_OPTIONS
变量值:-Dfile.encoding=UTF8

2、打开jenkins,系统管理--系统设置,在全局属性处勾选Environment variables,添加编码环境变量LANG=zh_CN.UTF-8 

3. 重启Jenkins

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值