一、生成测试报告 from base.HTMLTestRunner import HTMLTestRunner import unittest,time class TestRunner: def runner(self): # 实例化测试套件 suite = unittest.TestSuite() suite.addTests(unittest.TestLoader().discover('selenium/ranzhi/test/',pattern='login_test.py')) # 创建一个时间戳 pre = time.strftime('%Y-%m-%d_%H-%M-%S') # 添加测试用例 # 创建报告文件 report = open('selenium/ranzhi/report/report_%s.html'%pre,mode='wb') # 创建用例运行器 test_runner = HTMLTestRunner(stream=report,title='Ranzhi自动化测试报告',description='报告的详细内容...') # 运行报告 test_runner.run(suite) if __name__ == "__main__": TestRunner().runner() 二、自动发送Email from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import smtplib # 设置邮件服务器地址 smtpserver = 'smtp.163.com' # 设置邮件服务器端口号 port = 25 #发件人地址 sender = 'd1637386107@163.com' # 密码或授权码 password = 'HGCKFIBFYWRIPGCC' #收件人 # 创建邮件对象 mail = MIMEMultipart() # 初始化发件人 mail['from'] = sender # 添加收件人 mail['to'] = receivers #添加主题 mail['subject'] = 'Ranzhi自动化测试报告' # 读取附件 path = r'selenium\ranzhi\report\report_2021-03-02_16-19-48.html' with open(path,'rb') as file: report = file.read() # 对附件进行编码 attachment = MIMEText(report,'base64','utf-8') # 设置附件的类型 attachment['Content-Type'] = 'application/octet-stream' # 是指附件的处理方式 attachment['Content-Dispositione'] = 'attachment;filename=%s'%path.split('/')[-1] # 添加附件 mail.attach(attachment) # 生成邮件正文 content = ''' <p>Dear <b>Mike</b>,</p> <p> 这里是<u>Ranzhi</u>项目的测试报告,请您查收!</p> <p>此致</p> <p>Tom Cruse</p> ''' #对邮件正文进行编码 body = MIMEText(content,'html','utf-8') # 添加正文 mail.attach(body) #创建SMTP对象 smtp = smtplib.SMTP() # 连接服务器 smtp.connect(smtpserver,port) # 登录服务器 smtp.login(sender,password) # 发送邮件 smtp.sendmail(sender,receivers.split(';'),mail.as_string()) # 关闭服务器 smtp.close() print('邮件发送完毕!')
|