首页 > 编程语言 > 详细

python发报告模版

时间:2021-06-29 23:06:16      阅读:26      评论:0      收藏:0      [点我收藏+]
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‘] = "这是ssp项目自动化测试报告"
msg[‘From‘] = sender
msg[‘to‘] = receiver
# 通过os获取文件路径
#current_path = os.getcwd() # 获取当前脚本所在的文件夹路径
annex_path = os.path.abspath(os.path.dirname(os.getcwd()))+‘\\baipy\\report‘
# 附件内容的路径
a = os.path.join(annex_path,"report.html")
annex = open(a, "r", encoding="utf-8").read()

#body = ‘<pre><h1>自动化测试报告,请查收`</h1></pre>‘ # 正文内容的路径
main_body = ‘<pre><h1>自动化测试报告,请查收`</h1></pre>‘

# 添加正文到容器
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()
case_path = os.path.join(dir,‘case‘)
# case_dir = "C:\\Users\\Administrator\\PycharmProjects\\baipy\\case"
discover = unittest.defaultTestLoader.discover(
case_path,
pattern="test*.py"
)
return discover
if __name__ == ‘__main__‘:
result_path = "C:\\Users\\Administrator\\PycharmProjects\\baipy\\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, "2753730929@qq.com", "bshrqrytzziodeef", "897472853@qq.com")

python发报告模版

原文:https://www.cnblogs.com/WJ0123/p/14951703.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!