1说明
1电脑必须安装java环境
2电脑安装ant环境
3脚本必须正常使用
4 发送邮件必须有mail.jar、commons-email-1.5.jar、activation.jar,这些jar可以在阿里云仓库中下载对应的jar
5邮箱必须开启smtp
6修改jmeter下bin目录中的配置文件jmeter.properties
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
# Only available with HttpClient4
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
# Only available with HttpClient4
jmeter.save.saveservice.sent_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
7修改build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="ant-jmeter-test" default="run" basedir="F:\work\jmeter\apache-jmeter-5.4.3\extras">
<tstamp>
<format property="time" pattern="yyyyMMddhhmm" />
</tstamp>
<!-- 需要改成自己本地的 Jmeter 目录-->
<property name="jmeter.home" value="F:\work\jmeter\apache-jmeter-5.4.3" />
<!-- jmeter生成jtl格式的结果报告的路径-->
<property name="jmeter.result.jtl.dir" value="F:\work\jmeter\apache-jmeter-5.4.3\jtl" />
<!-- jmeter生成html格式的结果报告的路径-->
<property name="jmeter.result.html.dir" value="F:\work\jmeter\apache-jmeter-5.4.3\html" />
<!-- 生成的报告的前缀-->
<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}.html" />
<!--接受测试报告的邮箱-->
<property name="mail_from" value="abc@qq.com"/>
<property name="mail_to" value="abc@qq.com"/>
<target name="run">
<antcall target="test" />
<antcall target="report" />
<antcall target="sendEmail" />
</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="F:\work\jmeter\apache-jmeter-5.4.3\demojmx" 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"/></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}"/>
</xslt>
<!-- 因为上面生成报告的时候,不会将相关的图片也一起拷贝至目标目录,所以,需要手动拷贝 -->
<copy todir="${jmeter.result.html.dir}">
<fileset dir="${jmeter.home}/extras">
<include name="collapse.png" />
<include name="expand.png" />
</fileset>
</copy>
</target>
<!--发送邮件-->
<target name="sendEmail">
<mail mailhost="smtp.qq.com" mailport="587" subject="Test Report" messagemimetype="text/html" user="abc@qq.com" password="abc">
<from address="${mail_from}"/>
<to address="${mail_to}"/>
<message>This email was sent aytomatically by ANT. Please check the automation test report.Thank you!</message>
<attachments>
<fileset dir="${jmeter.result.html.dir}">
<include name="${ReportName}${time}.html"/>
<include name="collapse.png" />
<include name="expand.png" />
</fileset>
</attachments>
</mail>
</target>
</project>
二 启动ant
F:\work\jmeter\apache-jmeter-5.4.3\extras
ant run
三 查看报告文件
四 目前发邮件的功能暂未成功,一直无法读取到文件内容,暂时也没有弄明白问题出在哪里