载入HTMLTestRunner
# 获取当前时间 now = time.strftime("%Y-%m-%M-%H_%M_%S", time.localtime(time.time())) # 保存生成报告的路径 report_path= "D:\\Users\\wqa\\PycharmProjects\\notification\\report\\"+now+ u"客服系统UI测试报告" +"result.html" fp = open(report_path,"wb") runner =HTMLTestRunner.HTMLTestRunner(stream=fp,title=u‘客服系统UI测试报告‘,description=u‘用例执行情况:‘) #执行测试用例 runner.run(suite) fp.close() print(u‘测试报告生成地址:‘ + report_path) time.sleep(5) #发邮件 send_email(report_path)
发送email代码
def send_email(test_report): with open(test_report, ‘r‘, encoding=‘utf-8‘) as f: mail_body = f.read() f.close() mail_host = "smtp.163.com" # 设置服务器 mail_user = "3333@163.com" # 用户名 mail_pass = "222" # 口令 sender = ‘3333@163.com‘ receivers = [‘3333@qq.com‘] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 message = MIMEText(mail_body, ‘html‘, ‘utf-8‘) #message = MIMEText(‘Python 邮件发送测试...‘, ‘plain‘, ‘utf-8‘) message[‘From‘] = ‘333@163.com‘ message[‘To‘] = ‘333@qq.com‘ subject = ‘UI自动化测试报告‘ message[‘Subject‘] = Header(subject, ‘utf-8‘) try: smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465 smtpObj.login(mail_user, mail_pass) # 登录验证 smtpObj.sendmail(sender, receivers, message.as_string()) # 发送 print("mail has been send successfully.") except smtplib.SMTPException as e: print(‘发送邮件异常‘)
原文:https://www.cnblogs.com/seven7777/p/11890628.html