Web自动化【8】——python发送邮件示例(执行案例,生成HTML报告,发送测试报告邮件)

目录展示:
在这里插入图片描述
代码:

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('***@qq.com', '授权码')
    smtp.sendmail('***@qq.com', '***@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'
	# 在测试案例目录加载以test开头的案例集
    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)

最终效果图如下:
在这里插入图片描述
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章