# -*- coding:utf-8 -*-
import unittest, os
from commen import HTMLTestRunner
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os
def send_email(smtpserver, port, sender, psw, receiver):
# 写信模板
msg = MIMEMultipart()
msg['Subject'] = "这是项目自动化测试报告"
msg['From'] = sender
msg['to'] = receiver
# 通过os获取文件路径
current_path = os.getcwd() # 获取当前脚本所在的文件夹路径
report_path = os.path.join(current_path, "report")
annex_path = os.path.join(report_path, "report.html") # 附件内容的路径
annex = open(annex_path, "r", encoding="utf-8").read()
main_body = 'xx领导好,自动化测试报告,请查收,自动发送,不用回复!'
# main_body = open(body, "r", encoding="utf-8").read()
# 添加正文到容器
body = MIMEText(main_body, "html", "utf-8")
msg.attach(body)
# 添加附件到容器
att = MIMEText(annex, "base64", "utf-8")
att["Content-Type"] = "application/octet-sream"
att["Content-Disposition"] = 'attachment;filename="ssp_test_report.html"'
msg.attach(att)
# 连接发送邮件
smtp = smtplib.SMTP_SSL(smtpserver, port)
smtp.login(sender, psw)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
def all_case():
dir = os.getcwd()
# print(dir)
case_path = os.path.join(dir, 'case')
# case_dir = "D:\\code\\0517\\test\\case"
discover = unittest.defaultTestLoader.discover(
case_path,
pattern="test*.py" # 执行test开头的py文件
)
return discover
if __name__ == '__main__':
cu_path = os.getcwd()
report_path = os.path.join(cu_path, "report")
result_path = os.path.join(report_path, "report.html")
# result_path = "D:\\xxx\\xxx\\xxx\\report\\report.html"
fb = open(result_path, "wb")
runner = HTMLTestRunner.HTMLTestRunner(stream=fb,
title="xx测试报告",
description="自动化测试用例执行情况")
runner.run(all_case())
fb.close()
# 发送邮件
send_email("smtp.qq.com", 465, "108xx@qq.com", "xxx", "109@xxqq.com")
提前要设置好
文件相互关联进行