#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/10/16 16:02 # @Author : zhouyang # @File : run_bbs_test.py # _author_ = "janehost" import os import smtplib import sys import time import unittest from email.mime.text import MIMEText from HTMLTestRunner import HTMLTestRunner # =========================邮件接收者============================ #mailto_list = ["*****@qq.com"] mailto_list='*****@qq.com' #============= 设置服务器,用户名、口令以及邮箱的后缀=============== mail_host = "smtp.126.com" mail_user = "*****@126.com" mail_pass = "*****" #授权码 #===========================发送邮件============================ def send_mail(to_list, file_new): """ to_list: 发给谁 sub: 主题 content:内容 send_mail("*******@126.com","商城测试报告","主要功能回归测试") """ f = open(file_new, 'rb') mail_body = f.read() f.close() me = mail_user msg = MIMEText(mail_body, 'html', 'utf-8') msg['Subject'] = u'自动化测试报告' msg['From'] = me #msg['To'] = ",".join(to_list) msg['To'] =to_list try: s = smtplib.SMTP() s.connect(mail_host, 25) s.login(mail_user, mail_pass) s.sendmail(me, to_list, msg.as_string()) s.close() return True except Exception as e: print (str(e)) return False # ==============查找测试报告目录,找到最新生成的测试报告文件========== def new_report(testreport): lists = os.listdir(testreport) lists.sort(key=lambda fn:os.path.getatime(testreport + "\" + fn)) file_new = os.path.join(testreport, lists[-1]) print (file_new) return file_new if __name__ == '__main__': now = time.strftime("%Y-%m-%d %H_%M_%S ") filename = './bbs/report/' + now + 'result.html' fp = open(filename, 'wb') runner = HTMLTestRunner(stream=fp, title=u'商城自动化测试报告', description=u'环境:windows 7 浏览器:Chrome') discover = unittest.defaultTestLoader.discover('./bbs/test_case', pattern='TestCase.py') runner.run(discover) fp.close() file_path = new_report('./bbs/report/') if send_mail(mailto_list, file_path): print (u"发送成功") else: print (u"发送失败")
如果收不到邮件可以去邮箱的垃圾箱看看,若垃圾箱有说明成功,只是qq邮箱拦截了,可以设置白名单