Jmeter+ant测试报告发送邮件
jmeter+ant生成测试报告后发送给指定邮件地址
废话不多说进入主题:
一:环境部署
JDK下载安装配置:
jdk-8-windows-x64_8.0.1440.1.exe下载,提取码6666
JMETER下载安装配置:
apache-jmeter-5.1.1.zip下载,提取码6666
ANT下载安装配置:
apache-ant-1.10.2-bin.zip下载,提取码6666
二:文件配置
1.ant-jmeter-1.1.jar配置
JMETER安装目录D:\JMETER\apache-jmeter-3.3\extras下找到ant-jmeter-1.1.1.jar复制粘贴到ANT的D:\ant\apache-ant-1.10.5\lib下
2.将这三个JIA包放到ANT的lib目录下:
mail.jar下载,提取码:6666
commons-email-1.2.jar 下载,提取码:6666
activation.jar下载,提取码:6666
3.修改D:\software\apache-jmeter-3.3\bin目录下jmeter.properties文件配置
打开JMETER的bin目录下jmeter.properties文件
#jmeter.save.saveservice.output_format=csv改为:
jmeter.save.saveservice.output_format=xml
4.修改D:\software\apache-jmeter-3.3\bin目录下jmeter.properties文件如下部分,我这里都修改成true,这样执行完脚本后就会保存这些结果到.jtl文件里面:
jmeter.save.saveservice.data_type=true
jmeter.save.saveservice.label=true
jmeter.save.saveservice.response_code=true
# response_data is not currently supported for CSV output
jmeter.save.saveservice.response_data=true
# Save ResponseData for failed samples
jmeter.save.saveservice.response_data.on_error=false
jmeter.save.saveservice.response_message=true
jmeter.save.saveservice.successful=true
jmeter.save.saveservice.thread_name=true
jmeter.save.saveservice.time=true
jmeter.save.saveservice.subresults=true
jmeter.save.saveservice.assertions=true
jmeter.save.saveservice.latency=true
jmeter.save.saveservice.connect_time=true
jmeter.save.saveservice.samplerData=true
jmeter.save.saveservice.responseHeaders=true
jmeter.save.saveservice.requestHeaders=true
jmeter.save.saveservice.encoding=false
jmeter.save.saveservice.bytes=true
jmeter.save.saveservice.url=true
jmeter.save.saveservice.filename=true
jmeter.save.saveservice.hostname=true
jmeter.save.saveservice.thread_counts=true
jmeter.save.saveservice.sample_count=true
jmeter.save.saveservice.idle_time=true
5.修改D:\software\apache-jmeter-3.3\extras目录下build.xml配置
(build.xml所在路径:D:\software\apache-jmeter-3.3\extras)
直接复制下面内容到对应的build.xml中,修改:
A.邮箱部分
B.本地的jmeter地址
C.生成报告的目录地址
D.执行的脚本地址
<?xml version="1.0" encoding="UTF-8"?>
<project name="ant-jmeter" default="run" basedir=".">
<property name="jmeterPath" value="D:\software\apache-jmeter-3.3" />
<!-- 邮箱相关 -->
<property name="mailhost" value="邮箱服务" />
<property name="mailsubject" value="阿拉蕾知识库-接口自动化测试报告" />
<property name="mail_port" value="邮箱服务端口号" />
<property name="username" value="服务对应的邮箱" />
<property name="password" value="有相对应的**" />
<property name="mailfrom" value="发件人邮箱地址" />
<property name="mail_to" value="收件人1,收件人2,收件人3"/>
<property name="message" value="HI!,请查收,这是阿拉蕾知识库B端接口测试报告,如有任何疑问请联系我,谢谢!!!" />
<tstamp>
<format property="time" pattern="_yyyy_MMdd_HHmmss" />
</tstamp>
<property name="basedirectory" value="D:\software\apache-jmeter-3.3" />
<!-- 需要改成自己本地的jmeter目录 -->
<property name="jmeter.home" value="${jmeterPath}" />
<!-- 报告标题名称 -->
<property name="report.title" value="接口报告" />
<!-- jmeter生成jtl格式的结果报告的路径-->
<property name="jmeter.result.jtl.dir" value="${basedirectory}\report" />
<!-- jmeter生成html格式的结果报告的路径-->
<property name="jmeter.result.html.dir" value="${basedirectory}\report" />
<!-- 生成的报告的前缀-->
<property name="ReportName" value="APIreport" />
<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" />
<!-- jmeter生成jtl/html格式的结果报告的路径
<property name="jmeter.result.dir" value="D:\software\apache-jmeter-5.1.1\result" /> -->
<!-- 生成的报告的前缀
<property name="ReportName" value="展车管理" />
<property name="jmeter.result.jtlName" value="${jmeter.result.dir}/${ReportName}.jtl"/>
<property name="jmeter.result.htmlName" value="${jmeter.result.dir}/${ReportName}.html" />-->
<target name="run">
<antcall target="test"/>
<antcall target="report"/>
<antcall target="sendEmail"/>
</target>
<target name="test">
<echo>API_TEST</echo>
<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-3.3" includes="*.jmx"/>
<property name="jmeter.save.saveservice.output_format" value="xml"/>
</jmeter>
</target>
<!--html报告中时间显示为NAN,原因是ant中确实了两个lib库,xalan-2.7.2.jar和serializer-2.7.2.jar,将这两个库拷贝到ant的lib目录下-->
<path id="xslt.classpath">
<fileset dir="${jmeter.home}/lib" includes="xalan*.jar"/>
<fileset dir="${jmeter.home}/lib" includes="serializer*.jar"/>
</path>
<!--生成HTML测试报告-->
<target name="report">
<echo>API_TEST</echo>
<tstamp>
<format property="report.datestamp" pattern="yyyy/MM/dd HH:mm"/>
</tstamp>
<xslt
classpathref="xslt.classpath"
force="true"
in="${jmeter.result.jtlName}"
out="${jmeter.result.htmlName}"
style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl"> <!-- 报告模板路径 -->
<param name="dateReport" expression="${report.datestamp}"/>
<param name="titleReport" expression="${report.title}:${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>
<path id="lib_classpath">
<fileset dir="${basedir}/">
<include name="mail*.jar" />
<include name="activation*.jar" />
<include name="commons-email*.jar" />
<include name="ant-contrib*.jar" />
</fileset>
</path>
<!--自动发送邮件-->
<target name="sendEmail">
<echo>发送自动化测试报告</echo>
<mail
mailhost="${mailhost}"
mailport="${mail_port}"
charset="UTF-8"
ssl="true"
subject="${mailsubject}"
messagemimetype="text/html"
user="${username}"
password="${password}"
tolist="${mail_to}">
<from address="${mailfrom}" />
<attachments>
<fileset dir="${jmeter.result.html.dir}">
<include name="${ReportName}${time}.html" />
<include name="collapse.png" />
<include name="expand.png" />
<!-- -->
</fileset>
</attachments>
<message>${message}</message>
</mail>
</target>
</project>
6.执行测试
命令行cd到build.xml文件所在目录,输入 ant run
,执行测试,如图
看到build successful 则说明构建测试成功
打开html报告:默认模板
7.测试报告模板更换
Jmeter默认的报告展示的信息比较少,如果出错了,不是很方便定位问题。其实由.jtl格式转换为.html格式的报告过程中,style文件起了很关键的作用。下面介绍另一种style文件的使用方法:
A、下载style文件:jmeter.results.shanhe.me.xsl
B、把下载的文件放到jmeter的extras目录下(D:\software\apache-jmeter-3.3\extras)。
C、修改build.xml
D、执行测试,生成报告