ant+jmeter+jenkins 发送测试报告邮件

二、部署配置

2.1 jmeter

2.1.1 jar包二次开发:

将jmeter目录D:\apache-jmeter-5.0\extras中的ant-jmeter-1.1.1.jar包进行反编译。添加2个测试类 MailTask和JavaMail。具体代码参考文件中的java文件。然后进行编译生成新的jar包ant-jmeter-1.1.2-SNAPSHOT.jar。

2.1.2 添加build文件和测试文件

在jmeter目录D:\apache-jmeter-5.0\test下新增文件build.xml。

将需要测试的.jmx文件复制到目录D:\apache-jmeter-5.0\test。(与build.xml文件同目录)

2.1.3新增测试报告存储目录

在D:\apache-jmeter-5.0\test目录下新增文件目录report/jtl和report/html。这2个目录会存储测试执行后的报告文件。

2.2  ant

2.2.1 添加依赖jar包

在目录D:\apache-ant-1.10.5\lib下添加依赖jar包。javax.mail-1.5.5.jar,activation-1.1.1.jar,mail-2017.10.2.jar,ant-jmeter-1.1.2-SNAPSHOT.jar。

Jar包可以在二次开发时下载找到。

2.3 jenkins

2.3.1 下载war包部署至tomcat

下载jenkins包,直接在tomcat部署。点击start.sh 执行。详情请百度。

JavaMail 
package org.programmerplanet.ant.taskdefs.jmeter;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class JavaMail {

    static File mailAttachmentFile;

    static public void sendMail(int all, int successnum, int failnum,
                                String htmlstring, String mailSubject, String mailAttachment,
                                String mailAddress, String mailAddressCC, String mailHost,
                                String mailProtocol, String mailHostPort, String mailAddressFrom,
                                String mailAddressFromPwd,String jmeterProjectName,String testCaseStartTime)
            throws MessagingException, UnsupportedEncodingException {
        String s;
        String passStatus;//判定测试状态,测试通过,还是测试失败
        String failnumFormat = "";//处理失败数为0时,不要红色显示,失败数大于0时才显示
        DecimalFormat df = new DecimalFormat("0.00");
        String testCaseEndTime = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (all == 0) {
            s = "0";
            passStatus="无用例被执行";
        } else {
            s = df.format((float) successnum / (float) all * 100);
            if(failnum>0){
                passStatus = "<font color=red>Fail</font>";
                failnumFormat = "<font color=red>" + failnum + "</font>";
            }else {
                passStatus = "<font color=green>Pass</font>";
                failnumFormat = failnum + "";
            }
            //用例结束时间标注为发邮件时间
            testCaseEndTime = simpleDateFormat.format(new Date());
        }

        Properties props = new Properties();
        // 开启debug调试
//        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "flase");
        // 设置邮件服务器主机名(ip或者域名)
        props.setProperty("mail.smtp.host", mailHost);
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", mailProtocol);
        // 发送邮件协议端口
        props.setProperty("mail.smtp.port", mailHostPort);
        //props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//        props.setProperty("mail.smtp.ssl.enable", "true");//保证使用ssl时可以发送成功
        //props.setProperty("mail.smtp.socketFactory.port", "465");
        //props.setProperty("mail.smtp.socketFactory.fallback", "false");

        // 设置环境信息
        Session session = Session.getDefaultInstance(props,
                new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(mailAddressFrom,
                                mailAddressFromPwd);
                    }
                });
        // 创建邮件对象
        Message msg = new MimeMessage(session);

        // 设置发件人
        msg.setFrom(new InternetAddress(mailAddressFrom));

        // 设置收件人
        @SuppressWarnings("static-access")
        Address[] addresses = new InternetAddress().parse(mailAddress);
        @SuppressWarnings("static-access")
        Address[] addressesCC = new InternetAddress().parse(mailAddressCC);
        msg.setRecipients(Message.RecipientType.TO, addresses);// 直送对象
        msg.setRecipients(Message.RecipientType.CC, addressesCC);// 抄送对象
        // 设置主题
        if (mailSubject != null) {
            if(passStatus.equals("<font color=red>Fail</font>")){
                msg.setSubject("【自动化结果通知--Failure】:" + jmeterProjectName);
            }else if(passStatus.equals("<font color=green>Pass</font>")){
                msg.setSubject("【自动化结果通知--Successful】:" + jmeterProjectName);
            }
        } else {
            msg.setSubject("接口测试报告");
        }

        // 设置邮件内容

        BodyPart bp = new MimeBodyPart();
        Multipart mp = new MimeMultipart();
        bp.setContent(
                "<!DOCTYPE html>"
                        + "<html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">"
                        + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\" />"
                        + "<title>JMeter Test Results</title>"
                        + "<style type=\"text/css\">"
                        + "body {background: #ffffff;margin: 0 auto;padding: 0;text-align: left;font-size: 12px;font-family: \"微软雅黑\", \"宋体\";}"
                        + "table {font-size: 12px;font-family: \"微软雅黑\", \"宋体\";}"
                        + "td {line-height: 20px;}"
                        + "</style></head><body>"
                        + "<table width=\"800\" border=\"0\" cellspacing=\"0\" align=\"center\" cellpadding=\"0\" style=\"border: #ccc 1px solid;\">"
                        + "<tbody><tr><td>"
                        + "<table width=\"800\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#fbfbfb\" style=\"border-bottom: #eeeeee 1px solid;border-top: #cc0000 1px solid;\">"
                        + "<tbody><tr><td width=\"100\"><img src=\"http://www.uml.org.cn/jmshj/images/20131211411.jpg\" alt=\"测试结果\" width=\"100\" height=\"40\"></td>"
                        + "<td width=\"264\" align=\"right\" style=\"font-size: 12px; font-weight: bold; color: #999999;padding-top: 5px;\"><font color=\"#136ec2\" style=\"font-size:large\">Hi All <br></font>此信为系统邮件,请不要直接回复。</td></tr></tbody></table>"
                        + "<table width=\"750\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\" style=\"margin: 28px 28px 10px 28px;\">"
                        + "<tbody><tr><td style=\"font-size: 16px; color: #666666; padding-bottom: 6px;\" align=\"center\"><h2>接口测试报告</h2></td></tr></tbody></table>"
                        + "<table width=\"750\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\"><tbody><tr><td>"
                        + "<table width=\"750\" border=\"0\" align=\"center\" class=\"table_c\" style=\"border-collapse:collapse;\"><tbody><tr>"
                        + "<th width=\"100\" height=\"28\" align=\"center\" bgcolor=\"#FFFFFF\" style=\"border:1px solid #ccc;\">测试项目</th>"
                        + "<th width=\"70\" height=\"28\" align=\"center\" bgcolor=\"#FFFFFF\" style=\"border:1px solid #ccc;\">测试状态</th>"
                        + "<th width=\"70\" height=\"28\" align=\"center\" bgcolor=\"#FFFFFF\" style=\"border:1px solid #ccc;\">用例总数</th>"
                        + "<th width=\"70\" height=\"28\" align=\"center\" bgcolor=\"#FFFFFF\" style=\"border:1px solid #ccc;\">成功</th>"
                        + "<th width=\"70\" height=\"28\" align=\"center\" bgcolor=\"#FFFFFF\" style=\"border:1px solid #ccc;\">失败</th>"
                        + "<th width=\"70\" height=\"28\" align=\"center\" bgcolor=\"#FFFFFF\" style=\"border:1px solid #ccc;\">通过率</th>"
                        + "<th width=\"100\" height=\"28\" align=\"center\" bgcolor=\"#FFFFFF\" style=\"border:1px solid #ccc;\">开始时间</th>"
                        + "<th width=\"100\" height=\"28\" align=\"center\" bgcolor=\"#FFFFFF\" style=\"border:1px solid #ccc;\">结束时间</th></tr>"
                        + "<tr><td height=\"28\" bgcolor=\"#FFFFFF\" align=\"center\" style=\"border:1px solid #ccc;\">" + jmeterProjectName + "</td>"
                        + "<td height=\"28\" bgcolor=\"#FFFFFF\" align=\"center\" style=\"border:1px solid #ccc;\">" + passStatus + "</td>"
                        + "<td height=\"28\" bgcolor=\"#FFFFFF\" align=\"center\" style=\"border:1px solid #ccc;\">" + all + "</td>"
                        + "<td height=\"28\" bgcolor=\"#FFFFFF\" align=\"center\" style=\"border:1px solid #ccc;\">" + successnum + "</td>"
                        + "<td height=\"28\" bgcolor=\"#FFFFFF\" align=\"center\" style=\"border:1px solid #ccc;\">" + failnumFormat + "</td>"
                        + "<td height=\"28\" bgcolor=\"#FFFFFF\" align=\"center\" style=\"border:1px solid #ccc;\">" + s + "%</td>"
                        + "<td height=\"28\" bgcolor=\"#FFFFFF\" align=\"center\" style=\"border:1px solid #ccc;\">" + testCaseStartTime + "</td>"
                        + "<td height=\"28\" bgcolor=\"#FFFFFF\" align=\"center\" style=\"border:1px solid #ccc;\">" + testCaseEndTime + "</td></tr></tbody></table></td></tr></tbody></table></td></tr>"
                        + "<tr><td style=\"font-size: 12px; line-height: 20px; color: #999999; padding-top:15px; padding-left:25px \">备注:测试报告详细内容见邮件附件。</td></tr><tr><td>"
                        + "<table width=\"750\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\" style=\"margin: 28px 28px 10px 28px;\">"
                        + "<tbody><tr><td style=\"font-size: 12px; color: #666666; padding-bottom: 6px;\"><strong style=\"color:red\">失败用例:</strong></td></tr></tbody></table>"
                        + "<table width=\"750\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\"><tbody>"
                        + htmlstring    //失败的用例,如无用例失败,则显示无
                        + "</tbody></table>"
                        + "<table width=\"750\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\" style=\"margin: 28px 28px 10px 28px;\">"
                        + "<tbody><tr><td style=\"font-size: 12px; line-height: 20px; color: #999999; padding-top: 6px;\">如果您对本次邮件有任何疑问,请联系测试小组人员,感谢您的查阅!</td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></body></html>",
                "text/html;charset=utf-8");

        mp.addBodyPart(bp);

        // 附件为空时不发附件
        // mailAttachmentFile = new
        // File(System.getProperty("user.dir")+mailAttachment);
        mailAttachmentFile = new File(mailAttachment);
        if (mailAttachmentFile.exists()) {
            System.out.println("把mailAttachment报告文件作为附件发送");
            bp = new MimeBodyPart();
            // FileDataSource fileds = new
            // FileDataSource(System.getProperty("user.dir")+mailAttachment);
            FileDataSource fileds = new FileDataSource(mailAttachment);
            bp.setDataHandler(new DataHandler(fileds));
            bp.setFileName(MimeUtility.encodeText(fileds.getName(), "UTF-8",
                    "B"));
            mp.addBodyPart(bp);
        } else {
            System.out.println("mailAttachment文件不存在,邮件添加附件失败,请检查!");
        }

        msg.setContent(mp);
        msg.saveChanges();

        Transport transport = session.getTransport();
        // 连接邮件服务器
        transport.connect();
        // 发送邮件
        Transport.send(msg);
        // 关闭连接
        transport.close();
    }
}
build文件
<?xml version="1.0" encoding="GB2312"?>

<project name="ant-jmeter-test" default="run" basedir=".">
    <!--自动触发测试成功456-->
    <tstamp>
        <format property="time" pattern="yyyyMMddHHmmss" />
    </tstamp>
    <property environment="env"/>
    <!-- 需要改成自己本地的 Jmeter 目录-->
    <property name="jmeter.home" value="D:\apache-jmeter-5.0" />
    <!-- jmeter生成jtl格式的结果报告的路径-->
    <!--<property name="jmeter.result.jtl.dir" value="D:\apache-jmeter-3.1\demo\report\jtl" />-->
    <!-- jmeter生成html格式的结果报告的路径-->
    <!--<property name="jmeter.result.html.dir" value="D:\apache-jmeter-3.1\demo\report\html" />-->
    <!-- jmeter生成jtl、html格式的结果报告的路径-->
    <property name="jmeter.result.jtl.dir" value="D:\apache-jmeter-5.0\test\report\jtl" />
    <property name="jmeter.result.html.dir" value="D:\apache-jmeter-5.0\test\report\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" />
    <!-- GIT拉取到本地目录路径
    <property name="githome" value="${env.WORKSPACE}/接口自动化测试/接口测试" />-->

    <target name="run">
        <echo message="start..."/>
        <!--<antcall target="clean" />-->
        <antcall target="test" />
        <antcall target="report" />
        <antcall target="mail" />
    </target>
    <!--
    <target name="clean">
        <mkdir dir="${env.WORKSPACE}/results/${env.BUILD_ID}" />
    </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:\apache-jmeter-5.0\test" 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_30.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="mail">
        <taskdef name="javamail" classname="org.programmerplanet.ant.taskdefs.jmeter.MailTask"/>
        <javamail
            resultLog="${jmeter.result.jtlName}"
            mailAddressFrom=""
            mailAddressFromPwd=""
            jmeterProjectName="XXX项目接口测试"
            mailAddress="[email protected]"
            mailAddressCC="[email protected]"
            mailHost=""
            mailProtocol="smtp"
            mailHostPort=""
            mailSubject="${ReportName}${time}"
            mailAttachment="${jmeter.result.htmlName}">
        </javamail>
        <!--
        mailAddressFrom:发件人邮箱
        mailAddressFromPwd:发件人邮箱密码
        jmeterProjectName:本脚本主题是什么
        mailAddress:收件人邮箱,多个邮箱间逗号隔开
        mailAddressCC:抄送件人邮箱,多个邮箱间逗号隔开
        mailHost:发件人邮箱服务器地址
        mailProtocol:邮箱服务器协议
        mailHostPort:邮箱服务器端口
        resultLog:jtl文件,不需要修改
        mailSubject:html文件,不需要修改
        mailAttachment:附件,不需要修改
        -->

        <echo>发送报告邮件 at ${time}</echo>
    </target>

</project>

MailTask 类
package org.programmerplanet.ant.taskdefs.jmeter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.mail.MessagingException;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class MailTask extends Task {
    private String mailAddress = null;// 主送邮件对象地址
    private String mailAddressCC = null;// 抄送邮件对象地址
    private String mailSubject = null;// 邮件主题
    private String mailAttachment = null;// 邮件附件
    private String mailHost = null;// 邮件服务器
    private String mailProtocol = null;// 发送邮件协议
    private String mailHostPort = null;// 发送邮件协议的端口
    private String mailAddressFrom = null;// 发件人
    private String mailAddressFromPwd = null;// 发件人密码,是授权码
    private String jmeterProjectName = null;// jmeter脚本名字

    private String resultLog = null;
    File resultLogFile;
    File htmlFile;

    // jtl文件
    public void setResultLog(String resultLog) {
        this.resultLog = resultLog;
    }

    public String getResultLog() {
        return resultLog;
    }

    // 邮件主题
    public void setMailSubject(String mailSubject) {
        this.mailSubject = mailSubject;
    }

    public String getMailSubject() {
        return mailSubject;
    }

    // 附件 html报告文件
    public void setMailAttachment(String mailAttachment) {
        this.mailAttachment = mailAttachment;
    }

    public String getMailAttachment() {
        return mailAttachment;
    }

    // 收件地址,多个逗号(英文)隔开
    public void setMailAddress(String mailAddress) {
        this.mailAddress = mailAddress;
    }

    public String getMailAddress() {
        return mailAddress;
    }

    // 抄送邮件地址,多个逗号(英文)隔开
    public void setMailAddressCC(String mailAddressCC) {
        this.mailAddressCC = mailAddressCC;
    }

    public String getMailAddressCC() {
        return mailAddressCC;
    }

    public void setMailHost(String mailHost) {
        this.mailHost = mailHost;
    }

    public String getMailHost() {
        return mailHost;
    }

    public void setMailProtocol(String mailProtocol) {
        this.mailProtocol = mailProtocol;
    }

    public String getMailProtocol() {
        return mailProtocol;
    }

    public void setMailAddressFrom(String mailAddressFrom) {
        this.mailAddressFrom = mailAddressFrom;
    }

    public String getMailAddressFrom() {
        return mailAddressFrom;
    }

    public void setMailAddressFromPwd(String mailAddressFromPwd) {
        this.mailAddressFromPwd = mailAddressFromPwd;
    }

    public String getMailAddressFromPwd() {
        return mailAddressFromPwd;
    }

    public void setMailHostPort(String mailHostPort) {
        this.mailHostPort = mailHostPort;
    }

    public String getMailHostPort() {
        return mailHostPort;
    }

    public void setJmeterProjectName(String jmeterProjectName) {
        this.jmeterProjectName = jmeterProjectName;
    }

    public String getJmeterProjectName() {
        return jmeterProjectName;
    }

    /**
     * @see org.apache.tools.ant.Task#execute() task执行的入口
     */
    public void execute() throws BuildException {
        System.out.println("开始执行发送邮件task");
        // resultLogFile = new File(System.getProperty("user.dir") + resultLog);
        resultLogFile = new File(resultLog);
        if (mailAddress != null && resultLogFile.exists()) {

            System.out.println("开始解析resultLog");
            try {
                analyseResultLog();
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("resultLog不存在,请检查!");
        }
    }

    /**
     * 计算统计数据
     *
     * @throws MessagingException
     */
    private void analyseResultLog() throws BuildException, MessagingException {

        /*
         * String htmlString = "<tr valign=\"top\">" +
         * "<th width=\"50%\">接口</th>" + "<th>执行结果</th>" + "<th>执行时间</th>" +
         * "</tr>";
         */
        String htmlString = "<tr><th width=\"250\" height=\"28\" align=\"center\" bgcolor=\"#FFFFFF\" style=\"border:1px solid #ccc;\">接口</th>"
                + "<th width=\"65\" height=\"28\" align=\"center\" bgcolor=\"#FFFFFF\" style=\"border:1px solid #ccc;\">执行结果</th>"
                + "<th width=\"65\" height=\"28\" align=\"center\" bgcolor=\"#FFFFFF\" style=\"border:1px solid #ccc;\">执行时间</th></tr>";
        String time;
        String name;
        int count = 0;// 总用例数量
        String color;
        String color1 = "#FFFFFF";// 白色
        String color2 = "#FF0000";// 红色
        int successnum = 0;// 成功用例数量
        int failnum = 0;// 失败用例数量
        String testCaseStartTime = "";
        SimpleDateFormat simpleDateFormat = null;
        try {
            // FileInputStream fis = new
            // FileInputStream(System.getProperty("user.dir") + resultLog);
            FileInputStream fis = new FileInputStream(resultLog);
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                if (line.indexOf("<httpSample ") != -1) {
                    count = count + 1;// 统计用例个数
                    simpleDateFormat = new SimpleDateFormat(
                            "yyyy-MM-dd HH:mm:ss");
                    // 获取用例开始时间
                    if(count==1){
                        String startTimeMilTime = line.split("\"")[9];
                        testCaseStartTime = simpleDateFormat.format(Long
                                .parseLong(startTimeMilTime));
                    }
                    if (line.indexOf(" s=\"true\"") != -1) {
                        successnum = successnum + 1;
                        color = color1;
                        time = line.split("\"")[1];
                        name = line.split("\"")[13];
                        //不显示成功的用例
                        /*htmlString = htmlString
                                + "<tr><td height=\"28\" bgcolor=\"#FFFFFF\" align=\"center\" style=\"border:1px solid #ccc;\">"
                                + name
                                + "</td>"
                                + "<td height=\"28\" bgcolor=\""
                                + color
                                + "\" align=\"center\" style=\"border:1px solid #ccc;\">成功</td>"
                                + "<td height=\"28\" bgcolor=\"#FFFFFF\" align=\"center\" style=\"border:1px solid #ccc;\">"
                                + time + "ms</td></tr>";*/
                    } else {
                        failnum = failnum + 1;
                        color = color2;
                        time = line.split("\"")[1];
                        name = line.split("\"")[13];
                        htmlString = htmlString
                                + "<tr><td height=\"28\" bgcolor=\"#FFFFFF\" align=\"center\" style=\"border:1px solid #ccc;\">"
                                + name
                                + "</td>"
                                + "<td height=\"28\" bgcolor=\"#FFFFFF\" align=\"center\" style=\"border:1px solid #ccc;\"><font color="
                                + color
                                + ">失败</font></td>"
                                + "<td height=\"28\" bgcolor=\"#FFFFFF\" align=\"center\" style=\"border:1px solid #ccc;\">"
                                + time + "ms</td></tr>";
                    }
                }
            }

            // 无失败用例的时候,直接显示无即可
            if (failnum <= 0) {
                htmlString = "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;~~~太棒啦,没有失败的用例哦&nbsp;&nbsp;^_^</td></tr>";
            }
            if (successnum + failnum > 0) {
                // 有http请求才发邮件
                JavaMail.sendMail(successnum + failnum, successnum, failnum,
                        htmlString, mailSubject, mailAttachment, mailAddress,
                        mailAddressCC, mailHost, mailProtocol, mailHostPort,
                        mailAddressFrom, mailAddressFromPwd, jmeterProjectName,
                        testCaseStartTime);
                System.out.println("邮件发送成功");

            }
            br.close();
            isr.close();
            fis.close();
        } catch (IOException e) {
            throw new BuildException("Could not read jmeter resultLog: "
                    + e.getMessage());
        }
    }

}

原文地址:https://www.cnblogs.com/timezhang/p/10251104.html

时间: 2024-10-24 15:51:13

ant+jmeter+jenkins 发送测试报告邮件的相关文章

Ant+jmeter+jenkins 持续集成设置

1.创建任务http://i2.51cto.com/images/blog/201802/24/a5a104abf5fba828adab8bda5194ab38.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=" alt="Ant+jmeter+jenkins 持续集成设

ant+jmeter 实现自定义测试报告并发送邮件

1.安装ant 1.下载地址:http://www.apache.org/dist/ant/binaries/具体下哪个根据你的jdk版本去下,不要高于jdk的版本,我的jdk版本是1.8.0,版本,下载了 apache-ant-1.10.6-bin.zip 2.解压文件 unzip apache-ant-1.10.6-bin.zip 3.配置环境变量两种方式1)vi /etc/profile(永久的) 添加如下内容: ANT_HOME=/data/opt/jenkins/apache-ant-

ant jmeter jenkins 实现自动化测试

编者按: 本文核心的关键是ant去调用build.xml 执行自动化测试任务. bulid.xml 文件的内容按照注释修改为你自己服务器相关信息. 配置完成后一定要手动在服务器上执行一次,确保你配置的没有问题,在去配置jenkins服务. 我曾经就因为没有手动执行一下,导致卡了很长时间. ant 安装及配置 ant有两个分支版本:1.9x支持的是java5,1.10.x支持的是java8的版本. 这里我们选择apache-ant-1.10.1-bin.tar.gz的版本. cd /usr/loc

Ant+jmeter+jenkins搭建测试的持续集成

前提: Ant+jmeter 已经搭建完成并成功运行(参看ant+jmeter自动化性能测试) Jenkins在本地已经安装可运行(参看上一篇) 1.下载Jenkins安装 2.浏览器输入地址http://localhost:8080/显示界面,安装成功 3.新建一个任务并配置 构建一个自由风格的软件项目即可. 4.配置参数 旧的构建保存了你在一次构建中的所有临时文件,如果构建没有保留的必要,就勾选丢弃旧的构建,同时设置保持构建天使和保持构建的最大个数两个参数. PS:三个设置必须同时设置,否则

ant+jmeter+jenkins+git持续集成以及邮件报告展示

前序准备工作: ant--下载地址:http://ant.apache.org/bindownload.cgi jmeter--下载地址:http://jmeter.apache.org/download_jmeter.cgi jenkins--下载地址:https://jenkins.io/ git--下载地址:https://git-scm.com/downloads jdk--http://www.oracle.com/technetwork/java/javase/downloads/i

Ant+Jmeter+Jenkins集成

准备工作 安装配置Java环境 安装Tomcat和Jenkins 安装Ant 配置 1.将Jmeter安装目录apache-jmeter-3.0\extras文件夹中的ant-jmeter-1.1.1.jar复制到Ant安装目录下的lib文件夹下. 2.修改Jmeter配置文件jmeter.properties 修改jtl文件格式   添加jtl文件结果参数   3.编写ant的build.xml 1 <?xml version="1.0" encoding="UTF-

centos7搭建ANT+jmeter+jenkins接口测试自动化环境

一.环境准备 因为用到了jmeter和apache-tomcat,centos7必须要有java环境,所以配置jdk和apache-tomcat什么的,就不多说了,自行操作 帮你们偷懒: ant下载地址:http://mirrors.hust.edu.cn/apache//ant/binaries/apache-ant-1.10.3-bin.tar.gz jmeter下载地址:http://mirror.bit.edu.cn/apache//jmeter/binaries/apache-jmet

ant +jmeter+jenkins接口自动化测试二

[Jmeter篇]Jmeter+Ant+Jenkins接口自动化测试集成之半路逆转(二) 橙子探索测试发表于橙子探索测试订阅 72 [Jmeter篇]jmeter+Ant+Jenkins接口自动化测试集成(一) 一.更改方案 由于上一篇文章[Jmeter篇]jmeter+Ant+Jenkins接口自动化测试集成(一)中build.xml在cmd下执行ant正常且生成jtl文件和html文件,但是在jenkins构建多次一直失败,未能生成jtl文件和html文件,偶尔成功1次,未能找到解决原因,所

Python + HTMLTestRunner + smtplib 完成测试报告生成及发送测试报告邮件

一下代码是自己结合教材,并结合以往用到的实例编写的代码,可以做为参考 import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from HTMLTestRunner import HTMLTestRunner from email.header import Header import unittest import time,os #==========