1.咱们已经完成了selenium生成测试报告,那咱们要作将测试报告发送给相关人员,就能够这样使用html
#coding: utf - 8from case.t_single import Sinfrom case.test_baidu import Ceshifrom case.test_bokeyuan import Bofrom unittest import TestSuite,TestLoaderimport unittest,time,osimport HTMLTestRunnerimport smtplib#发送邮件模块from email.mime.text import MIMEText #定义邮件neirfrom email.header import Header #定义邮件标题from email.mime.multipart import MIMEMultipartdef run_main(): suit = unittest.TestSuite() suit.addTest(Bo('test_shouye')) suit.addTest(Ceshi('test_search')) suit.addTest(Ceshi('test_home')) now=time.strftime('%Y%m%d%H%M',time.localtime(time.time())) #'%Y%m%d%H%M',time.localtime(time.time()) file=r'report/report.html' #测试报告地址 fb=open(file,'wb') runner = HTMLTestRunner.HTMLTestRunner(stream=fb,title='test_report',description='detail') runner.run(suit) fb.close() with open(file,'rb') as ob: main_body=ob.read() msg=MIMEMultipart() #添加容器 body=MIMEText(main_body,'html','utf-8') #邮件正文 msg['Subject']=u'测试完成' msg['sender']='xxxx' #发件人 msg['receiver'] = 'xxx' #收件人 msg.attach(body) #添加附件 att=MIMEText(main_body,'bs64','utf-8') att['Content-Type']="application/octet-stream" att['Content-Disposition'] = 'attachment;filename="report.html"' msg.attach(att) try: smtp=smtplib.SMTP_SSL('smtp.qq.com',465) #扣扣邮件服务,端口=465 except: smtp=smtplib.SMTP() smtp.connect('smtp.qq.com',465) smtp.login(msg['sender'],'password=xxx') #邮件服务登录用户名,密码 smtp.sendmail(msg['sender'],msg['receiver'],msg.as_string()) #发送邮件 smtp.quit() print('mail send out')if __name__ == '__main__': run_main()