1.我们已经完成了selenium生成测试报告,那我们要做将测试报告发送给相关人员,就可以这样使用
#coding: utf - 8
from case.t_single import Sin
from case.test_baidu import Ceshi
from case.test_bokeyuan import Bo
from unittest import TestSuite,TestLoader
import unittest,time,os
import HTMLTestRunner
import smtplib#发送邮件模块
from email.mime.text import MIMEText #定义邮件neir
from email.header import Header #定义邮件标题
from email.mime.multipart import MIMEMultipart
def 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()