python+selenium+unittest测试框架4-邮件发送最新测试报告

邮件发送最新测试报告

示例:

复制代码
import HTMLTestRunner
import unittest
import os import time import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart def allcase(): '''加载测试用例''' discover = unittest.defaultTestLoader.discover(casepath, pattern="case*.py", top_level_dir=None) return discover def run_case(): '''执行测试用例,生成测试报告''' now = time.strftime("%Y_%m_%d %H_%M_%S") htmlreportpath = os.path.join(reportpath, now+"result.html") fp = open(htmlreportpath, "wb") runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"自动化测试报告", description=u"测试用例执行情况") # 调用allcase函数返回值  runner.run(allcase()) fp.close() def get_report_file(reportpath): '''获取最新的测试报告''' lists = os.listdir(reportpath) lists.sort(key=lambda fn: os.path.getmtime(os.path.join(reportpath, fn))) # 找到最新生成的报告文件 reportfile = os.path.join(reportpath, lists[-1]) return reportfile def send_mail(sender, psw, receiver, smtpserver,reportfile, port=465): '''发送最新的测试报告内容''' #打开测试报告 with open(reportfile, "rb") as f: mail_body = f.read() # 定义邮件内容 msg = MIMEMultipart() body = MIMEText(mail_body, _subtype='html', _charset='utf-8') msg['Subject'] = u"自动化测试报告" msg["from"] = sender msg["to"] = receiver msg.attach(body) # 添加附件 att = MIMEText(open(reportfile, "rb").read(), "base64", "utf-8") att["Content-Type"] = "application/octet-stream" att["Content-Disposition"] = 'attachment; filename= "report.html"' msg.attach(att) try: smtp = smtplib.SMTP_SSL(smtpserver, port) except: smtp = smtplib.SMTP() smtp.connect(smtpserver,port) # 用户名密码  smtp.login(sender, psw) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit() if __name__ == '__main__': casepath = os.path.join(os.getcwd(),"case") #测试用例存放路径 reportpath = os.path.join(os.getcwd(),"report") #测试报告存放路径 run_case() #执行测试用例 reportfile = get_report_file(reportpath) smtpserver = "smtp.qq.com" sender = "139271007@qq.com" # 自己的账号 psw = "password" #自己的密码 receiver = "386421542@qq.com" #对方的账号 send_mail(sender, psw, receiver, smtpserver,reportfile)
复制代码

转载于:https://www.cnblogs.com/txx403341512/p/9353841.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值