Web自动化【9】——生成HTML格式的report,并邮件发送

简单的测试用例执行,生成html格式的report,并当做附件进行邮件发送。

from HTMLTestRunner import HTMLTestRunner
import unittest
import time
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
import smtplib
import os


# 定义发送邮件
def send_mail(file):
    f = open(file, 'rb',).read()
    # f.close()
    att = MIMEText(f, 'base64', 'utf-8')
    att['content-type'] = 'application/octet-stream'
    att['content-disposition'] = "attachment; filename ='testing_result.html'"
    msg = MIMEText('各位好,附件是本次的测试报告,请查阅!谢谢', 'plain', 'utf-8')

    msg_all = MIMEMultipart('related')
    msg_all['Subject'] = Header('自动化测试报告', "utf-8")

    print('添加附件')
    msg_all.attach(att)
    print('添加成功')
    msg_all.attach(msg)

    smtp = smtplib.SMTP()
    smtp.connect('smtp.qq.com', 25)
    smtp.login('14*****@qq.com', '****')  #邮箱 &  密码
    smtp.sendmail('14*****@qq.com', '14*****@qq.com', msg_all.as_string())
    smtp.quit()
    print('email has send out!')


# 查找测试报告
def find_report(address):
    lists = os.listdir(address)
    # lists.sort(key=lambda fn: os.path.getmtime(address + '\\' + fn))
    file_new = os.path.join(address, lists[-1])
    print(file_new)
    return file_new


if __name__ == '__main__':
    test_dir = 'D:\\pypypy2\\zidonghua\\uinttest\\test_project\\test_case'
    address_report = 'D:\\pypypy2\\zidonghua\\uinttest\\test_project\\test_case\\report'

    discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py')

    now = time.strftime("%Y-%m-%d_%H_%M_%S")
    filename = address_report + '\\' + now + 'mail_report.html'
    fp = open(filename, 'wb')
    runner = HTMLTestRunner(stream=fp, title='测试报告', description='用例执行情况' )
    runner.run(discover)
    fp.close()

    new_report = find_report(address_report)
    print(new_report)
    send_mail(new_report)










  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值