发送测试报告邮件
from HTMLTestRunner import HTMLTestRunner
import unittest
from email.mime.text import MIMEText
from email.header import Header
import smtplib
import time
import os
#定义发送邮件
def send_mail(file_new):
f = open(file_new,"rb")
mail_body = f.read()
f.close()
#编写HTML类型邮件正文
msg = MIMEText(mail_body,"html","utf-8")
msg["Subject"] = Header("自动化测试报告","utf-8")
#发送邮箱服务器
smtp = smtplib.SMTP()
smtp.connect("smtp.qq.com")
#发送邮箱用户/QQ邮箱smtp的授权码
smtp.login("xxxx@qq.com","xxxxx")
#发送邮箱/接收邮箱/邮件主题
smtp.sendmail("xxx@qq.com","xxxx@qq.com",msg.as_string())
smtp.quit()
print("email has send out")
#查找测试报告目录,找到最新生成的测试报告文件
def new_report(testreport):
lists = os.listdir(testreport)
#重新按时间对目录下的文件进行排序
lists.sort(key=lambda fn: os.path.getmtime(testreport +"\\"+fn))
file_new = os.path.join(testreport,lists[-1])
print(file_new)
return file_new
if __name__ == "__main__":
#测试用例文件路径
test_dir = "E:\\Workspace\\test_case\\test_a"
#测试报告存放路径
test_report = "E:\\"
discover = unittest.defaultTestLoader.discover(test_dir,
pattern="test*.py")
#时间格式
Now = time.strftime("%Y-%m-%d %H_%M_%s")
filename = test_report + "\\" + Now + "测试.html"
fp = open(filename,"wb")
runner = HTMLTestRunner(stream=fp,
title="测试报告",
description="测试执行情况:")
runner.run(discover)
fp.close()
#发送测试报告
new_report = new_report(test_report)
send_mail(new_report)
原文地址:https://blog.csdn.net/weixin_45242451/article/details/93334756
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。