这里封装一个send_mail()方法,用于测试完成后读取测试报告内容并将测试结果发送邮件通知接收人
# coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from .logger import *
def send_email(test_report):
with open(test_report, 'r', encoding='utf-8') as f:
mail_body = f.read() # 打开测试报告,读取报告内容作为邮件内容
sender = '***********@139.com' # 发出邮箱
receiver = [*********@qq.com', *********@qq.com'] # 接受邮箱
mail_server = 'smtp.139.com' # 邮箱服务地址
subject = '自动化测试报告' # 邮件标题
username = '********' # 邮箱登录名
passwd = '*********' # 密码
message = MIMEText(mail_body, 'html', 'utf-8') # 设置邮件格式
message['Subject'] = Header(subject, charset='utf-8')
# message['Sender'] = Header(sender, charset='utf-8')
# 邮箱登录
smtp = smtplib.SMTP()
smtp.connect(mail_server)
smtp.login(username, passwd)
# 发送邮件
for i in receiver:
smtp.sendmail(sender, i, message.as_string())
Logger().get_log().info( '邮箱:' + i + ',发送邮件成功!\n') # 打印log
smtp.quit()
执行用例,在生成报告后调用此方法即可实现通过邮件发送测试结果