JMeter+Ant-自动发送测试结果报告邮件

build.xml文件

将这三个jar包(activation.jar、commons-email-1.2.jar、mail.jar)放到ant下的lib下

(我的是:/usr/local/Cellar/ant/1.10.5/libexec/lib)

<?xml version="1.0" encoding="UTF8"?>

<project name="ant-jmeter-test" default="run" basedir=".">

<property name="jmeterPath" value="/Applications/apache-jmeter-4.0"/>
<property name="mailhost" value="smtp.163.com"/>
<property name="username" value="jiangpr_ok@163.com"/>
<property name="password" value="输入163邮箱等密码"/>
<property name="mailfrom" value="jiangpr_ok@163.com"/>
<property name="mail_to" value="591379035@qq.com,931410265@qq.com"/>
<property name="mailsubject" value="XX系统接口自动化测试报告"/>
<property name="mail_port" value="25"/>
<property name="message" value="Hi!请查收下,这是XX接口自动化测试报告,如有任何疑问,请联系我,谢谢!"/>


    <tstamp>
        <format property="time" pattern="yyyyMMddhhmm" />
     </tstamp>
     <property name="jmeter.home" value="${jmeterPath}" />
     <property name="jmeter.result.jtl.dir" value="${jmeterPath}	est
eportjtl" />
     <property name="jmeter.result.html.dir" value="${jmeterPath}	est
eporthtml" />
     <property name="htmlReportNameSummary" value="TestReport" />
     <property name="jmeter.result.jtlName" value="${jmeter.result.jtl.dir}/${htmlReportNameSummary}${time}.jtl" />
     <property name="jmeter.result.htmlName" value="${jmeter.result.html.dir}/${htmlReportNameSummary}${time}.html" />

     
    <target name="run">
         <antcall target="test" />
         <antcall target="report" />
         <antcall target="sendEmail" />
     </target>
     
     <!--执行接口测试-->
     <target name="test">
          <echo>执行接口自动化测试</echo>
         <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
         <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}">
            <!--要执行的测试脚本-->
            <testplans dir="${jmeterPath}	estscript" includes="测试计划.jmx" />
             <property name="jmeter.save.saveservice.output_format" value="xml"/>
         </jmeter>
     </target>
     
     <!--解决报告中NAN字段显示问题-->
     <path id="xslt.classpath">
        <fileset dir="${jmeter.home}/lib" includes="xalan-2.7.2.jar"/>
        <fileset dir="${jmeter.home}/lib" includes="serializer-2.7.2.jar"/>
    </path>

     <!--生成HTML测试报告-->
     <target name="report">
     <echo>生成接口自动测试报告</echo>
         <xslt classpathref="xslt.classpath"
              force="true"
              in="${jmeter.result.jtlName}" out="${jmeter.result.htmlName}"
              style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl"  />
        
        <!--复制图片-->
        <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">
    <echo>发送自动化测试报告</echo>
         <mail mailhost="${mailhost}"
            ssl="ture"
            user="${username}" 
            password="${password}" 
            mailport="${mail_port}"
            subject="${mailsubject}" 
            messagemimetype="text/html" 
            tolist="${mail_to}">
         <from address="${mailfrom}" />
               <attachments>
                     <fileset dir="${jmeter.result.html.dir}">
                             <include name="${htmlReportNameSummary}${time}.html"/>
                             <include name="collapse.png" />
                             <include name="expand.png" />
                     </fileset>
               </attachments>
               <message>
               ${message}
               </message>
         </mail>
     </target>
 </project>

执行

(base) localhost:test ligaijiang$ cd /Applications/apache-jmeter-4.0/test
(base) localhost:test ligaijiang$ ant
Buildfile: /Applications/apache-jmeter-4.0/test/build.xml
(base) localhost:test ligaijiang$ cd /Applications/apache-jmeter-4.0/test

(base) localhost:test ligaijiang$ ant Buildfile: /Applications/apache-jmeter-4.0/test/build.xml run: test: [echo] 执行接口自动化测试 [jmeter] Executing test plan: /Applications/apache-jmeter-4.0/test/script/测试计划.jmx ==> /Applications/apache-jmeter-4.0/test/report/jtl/TestReport201904270811.jtl [jmeter] WARNING: An illegal reflective access operation has occurred [jmeter] WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/Applications/apache-jmeter-4.0/lib/xstream-1.4.10.jar) to field java.util.TreeMap.comparator [jmeter] WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields [jmeter] WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations [jmeter] WARNING: All illegal access operations will be denied in a future release [jmeter] Creating summariser <summary> [jmeter] Created the tree successfully using /Applications/apache-jmeter-4.0/test/script/测试计划.jmx [jmeter] Starting the test @ Sat Apr 27 20:11:43 CST 2019 (1556367103226) [jmeter] Waiting for possible Shutdown/StopTestNow/Heapdump message on port 4445 [jmeter] summary + 46 in 00:00:17 = 2.8/s Avg: 354 Min: 72 Max: 2209 Err: 0 (0.00%) Active: 1 Started: 1 Finished: 0 [jmeter] summary + 30 in 00:00:12 = 2.6/s Avg: 387 Min: 64 Max: 4197 Err: 8 (26.67%) Active: 0 Started: 1 Finished: 1 [jmeter] summary = 76 in 00:00:28 = 2.7/s Avg: 367 Min: 64 Max: 4197 Err: 8 (10.53%) [jmeter] Tidying up ... @ Sat Apr 27 20:12:11 CST 2019 (1556367131829) [jmeter] ... end of run report: [echo] 生成接口自动测试报告 [xslt] Processing /Applications/apache-jmeter-4.0/test/report/jtl/TestReport201904270811.jtl to /Applications/apache-jmeter-4.0/test/report/html/TestReport201904270811.html [xslt] Loading stylesheet /Applications/apache-jmeter-4.0/extras/jmeter-results-detail-report_21.xsl sendEmail: [echo] 发送自动化测试报告 [mail] Sending email: XX系统接口自动化测试报告 [mail] Sent email with 3 attachments BUILD SUCCESSFUL Total time: 33 seconds (base) localhost:test ligaijiang$
原文地址:https://www.cnblogs.com/jpr-ok/p/10780162.html