# coding=utf-8 import os,time import smtplib from email.mime.text import MIMEText import unittest from HTMLTestRunner import HTMLTestRunner #定义发送邮件 def send_mail(file_new): #发件邮箱 mail_from = '发件人邮箱' #收件邮箱 mail_to = '收件人邮箱' #定义正文 with open(file_new,'rb') as file: maile_body = file.read() file.close() msg = MIMEText(maile_body,_subtype='html',_charset='utf-8') #定义标题 msg['Subject'] = 'MP自动化测试报告' #定义发送时间 msg['date'] = time.strftime('%a,%d %b %Y %H:%M:%S %Z') smtp = smtplib.SMTP() #连接SMPT服务器 smtp.connect('smtp.tencent.com') #登入邮箱发送邮件 smtp.login('邮箱账号','密码') smtp.sendmail(mail_from,mail_to,msg.as_string()) smtp.quit() print('email has send out......') #定义测试报告文件 def send_report(testreport): # 定义文件目录 result_dir = testreport #result_dir = 'D:\\selenium_test_report' lists = os.listdir(result_dir) # 重新按时间对目录下的文件进行排序 lists.sort(key=lambda fn:os.path.getmtime(result_dir + "\\" + fn)) print('最新文件:' + lists[-1]) file_new = os.path.join(result_dir,lists[-1]) print(file_new) #调用发送邮件模块 send_mail(file_new) #将用例添加到测试套件 def creatsuite(): testunit = unittest.TestSuite() #定义测试文件查找的目录 test_dir = 'D:\\selenium_test_report' #定义descover方法的参数 discover = unittest.defaultTestLoader.discover(test_dir,pattern='test*.py',top_level_dir=None) #循环添加discover用例 for test_case in discover: print(test_case) testunit.addTest(test_case) return testunit if __name__ == "__main__": now = time.strftime("%Y-%m-%d %H-%M-%S") testreport = 'D:\\selenium_test_report' filename = testreport + now + 'result.html' with open(filename,'wb') as fp: runner = HTMLTestRunner(stream=fp,title='自动化测试报告',description='用例执行情况:') alltestnames = creatsuite() runner.run(alltestnames) fp.close() send_report(testreport)
selenium 自动发送测试报告
最新推荐文章于 2024-04-13 17:33:52 发布