selenium自动化测试之整合测试报告
标签(空格分隔): 整合报告
如下截图我们添加一个文件叫做:latest_report.py文件,
import time
import os
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import os
import unittest
from BSTestRunner import BSTestRunner
def send_email(latest_report):
f=open(latest_report,'rb')
mail_content = f.read()
f.close()
smtpserver = 'smtp.exmail.qq.com'
user = '****@zhan.com'
#这里是邮箱的授权码
password = '****'
sender = '****@zhan.com'
receiver = '****@qq.com'
# 发送邮件的标题和内容
subject = 'web selenium 自动化测试报告'
content = '<html><h1 style = "color:red">测试报告2017</h1></html>'
# 构建发送与接收信息
msgRoot = MIMEText(mail_content,'html','utf-8')
msgRoot['subject'] = Header(subject,'utf-8')
#这里填写发送邮箱,和收件邮箱
msgRoot['from'] = sender
msgRoot['To'] = receiver
# ssl 协议端口号要使用465
smtp = smtplib.SMTP_SSL(smtpserver, 465)
# 向用户表示用户的身份
smtp.helo(smtpserver)
# 服务器返回结果确认
smtp.ehlo(smtpserver)
# 登录邮箱服务器用户名和密码
smtp.login(user, password)
print("发送邮件")
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit
print("邮件发送完成")
def latest_report(report_dir):
# 如果不在同一个目录,要使用绝对路经,这里在同一个目录就使用相对路径了
lists = os.listdir(report_dir)
print(lists)
lists.sort(key=lambda fn: os.path.getatime(report_dir + "\" + fn))
print("the lastest report is " + lists[-1])
file = os.path.join(report_dir, lists[-1])
print(file)
if __name__ == '__main__':
test_dir ='./test_case'
report_dir='./test_report'
discover =unittest.defaultTestLoader.discover(test_dir,pattern='test*.py')
now =time.strftime('%Y-%m-%d %H_%M_%S')
report_name=report_dir +'/'+now+'result.html'
with open(report_name,'wb')as f:
runner =BSTestRunner(stream=f,title='TestReport',description="baidu search")
f.close()
#获取最新的测试报告
latest_report=latest_report(report_dir)
#发送邮件报告
send_email(latest_report)