美文网首页
httpRunner中加入自动发送邮件功能

httpRunner中加入自动发送邮件功能

作者: dr_go | 来源:发表于2019-03-30 16:53 被阅读0次

    参考文献:https://www.cjhang.com/2016/10/19/%E4%BD%BF%E7%94%A8python%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6/

    接口测试开源工具httprunner:http://www.wuzihan.top/post/69/

    python版本:2.7

    操作系统:mac

    自定义发送邮件类:

    #-*- coding:utf-8 -*-

    import os,smtplib

    import time

    import datetime

    from email.mime.textimport MIMEText

    from email.mime.multipartimport MIMEMultipart

    def sendMail(summary):

    # 第三方 SMTP 服务

        mail_host="smtp.gmail.com"  #设置服务器

        mail_user="**"    #登录邮件账号

        mail_pass="****"  #密码

        sender ="***"  #发送人

        receivers = ['**','**']# 收件人,这里写死,多个的话用逗号隔开

        cc = ['***']#抄送人,这里写死,多个的话用逗号隔开

    # 以下是发送邮件包含的内容

        mail_msg ='geofence模块接口测试报告如下:</br><br/>本次测试运行环境:QA;</br>&nbsp;&nbsp;&nbsp;共执行用例:&nbsp;'\

    +str(summary["stat"]["teststeps"]["total"])+'条;</br>&nbsp;&nbsp;&nbsp;失败:&nbsp;'\

    +str(summary["stat"]["teststeps"]["errors"])+'条;</br>&nbsp;&nbsp;&nbsp;成功:&nbsp;'\

    +str(summary["stat"]["teststeps"]["successes"])+"条;

    如需了解下详情,请参见附件测试报告!谢谢!"

        message = MIMEMultipart()

    to_addres =', '.join( receivers)

    message['From'] = sender

    message['To'] =  to_addres

    message['Cc'] =', '.join( cc)

    message.attach(MIMEText(mail_msg, 'html', 'utf-8'))

    report_dir_path = os.path.join(os.getcwd(), "reports")

    print report_dir_path

    html_report_name =str(int(summary['time']['start_at']))+'.html'

        # html_report_name = summary['time']['start_at'].strftime("%Y-%m-%d-%H-%M-%S") + '.html'

        report_path = os.path.join(report_dir_path, html_report_name)

    reportDate ="httprunner接口测试报告__"+datetime.datetime.fromtimestamp(summary['time']['start_at']).strftime("%Y-%m-%d-%H-%M-%S") +'.html'

        message['Subject'] = reportDate

    att2 = MIMEText(open(report_path, 'rb').read(), 'base64', 'utf-8')

    att2["Content-Type"] ='application/octet-stream'

        att2["Content-Disposition"] ='attachment; filename='+html_report_name

    message.attach(att2)# 发送时候拿到httprunner生成的报告,放到邮件的附件中

        try:

    s = smtplib.SMTP(mail_host,587)

    s.starttls()

    s.login(mail_user, mail_pass)

    s.sendmail(sender,receivers, message.as_string())

    # client = smtplib.SMTP(smtp_server, smtp_port)

    # client.starttls()

    #

    # client.login(username, password)

    # client.sendmail(sender, receivers.split(',') + cc.split(','), msg.as_string())

    # client.quit()

            print("邮件发送成功")

    except smtplib.SMTPException:

    print("Error: 无法发送邮件")

    finally:

    s.quit()

    第二步:

    在httprunner 的cli目录下,导入文件:

    from httprunnerimport sendMail

    main_hrun函数中增加:

    sum = runner.summary

    sendMail.sendMail( sum)

    相关文章

      网友评论

          本文标题:httpRunner中加入自动发送邮件功能

          本文链接:https://www.haomeiwen.com/subject/uslsbqtx.html