发送邮件功能

运行完成后,生成测试报告,报告以邮件附件方式发送

具体实现过程见代码及注释

import unittest, time, HTMLTestRunner
from email.mime.text import MIMEText
from email.header import Header
import smtplib,os
from email.mime.multipart import MIMEMultipart
  
# 发送邮件
def send_mail(new_file_dir, new_file_name, now):
  
    # 获取附件内容
    f = open(new_file_dir, 'rb')
    mail_body = f.read()
    f.close()
  
    # 创建一个带附件的实例
    msg = MIMEMultipart() # 如果没有附件,这行可以不要,就只定义 msg = MIMEText('本次测试详情见附件。','plain','utf-8')
  
    # 构造正文 
    body = MIMEText(mail_body,_subtype='html',_charset='utf-8') # 读取html附件内容作为邮件正文
    # body = MIMEText('本次测试详情见附件。','plain','utf-8') # 自定义邮件正文内容
    # body = MIMEText('<html><h1>你好!</h1></html>','html','utf-8')
    msg.attach(body)
  
    # 构造附件
    att = MIMEText(mail_body, 'base64','utf-8') # base64加密,utf-8格式发送
    att["Content-Type"] = 'application/octet-stream'
    att["Content-Disposition"] = 'attachment; filename=' + new_file_name # 如果文件名是固定的,就是下面的写法
    # att["Content-Disposition"] = 'attachment; filename=HelloWorld.html' # 这里的filename可以任意写,写什么名字,邮件中显示什么名字
    msg.attach(att)
  
    # 邮件头
    mail_from='xxxxx@126.com'
    # mail_to='xxxxx@126.com'
    mail_to = ['xxxxx@126.com'] # 多个收件人存放在列表中
    # msg['Subject']=u"自动化测试报告"
    msg['Subject']=u"xx项目自动化测试报告:" + now
  
    # 发送邮件
    smtp = smtplib.SMTP()
    smtp.connect('smtp.126.com')
    smtp.login('xxxxx@126.com','********')
    smtp.sendmail(mail_from,mail_to,msg.as_string())
    smtp.quit()
  
    print('email has send out !')
  
# 获取最新文件
def get_new_file(files):
    # print(files) # ./report/
    lists = os.listdir(files)
    lists.sort(key=lambda fn: os.path.getmtime(files+"\"+fn))
    file_ = os.path.join(files,lists[-1])
    print('最新的文件相对路径为:'+file_) # ./report/2017-05-31_11_56_18_report.html
    # print(file_) # ./report/2017-05-31_11_56_18_report.html
    print('最新的文件为:'+lists[-1]) # 最新的文件为:2017-05-31_12_08_48_report.html
    # print(lists[-1]) # 2017-05-31_11_56_18_report.html
    return file_, lists[-1]
  
# 获取指定目录下符合规则的文件
suit = unittest.defaultTestLoader.discover("./my_test_case", pattern='*.py') # 第二个参数是匹配文件名
  
if __name__ == '__main__':
      
    #获取当前时间
    now = time.strftime("%Y-%m-%d_%H_%M_%S") # 年月日之间最好不好有空格,因为后面作为参数传递当附件名时,空格后面的不会显示
    # print(now) # 2017-05-31_12_04_19
      
    #定义报告存放路径
    fp = open("./report/" + now + "_report.html", 'wb') # "b"表示处理二进制文件
      
    #定义测试报告
    runner = HTMLTestRunner.HTMLTestRunner( # HTMLTestRunner文件下的HTMLTestRunner类重写了unittest中的TextTestRunner类
        stream=fp,
        title=u'百度搜索测试报告', # 以u或U开头的字符串表示unicode字符串
        description=u'【运行环境及用例执行情况】:win7,Chrome;结果见邮件正文,如有fail且要查看详情,请下载附件查看,或者本机访问测试服务器查看,本机访问:http://<测试服务器IP>:8000/')
          
    # runner = unittest.TextTestRunner()
    # runner.run(suit)
  
    #运行测试用例
    runner.run(suit)
    #关闭报告文件
    fp.close()
  
    # send_mail("./report/2017-05-30 17_15_39_report.html")
    new_file_dir, new_file_name = get_new_file("./report/")
    send_mail(new_file_dir, new_file_name, now) # 第一个参数是附件目录用于读文件;第二个参数是文件名,用户展示附件名;第三个参数是时间,用于邮件标题
    # 上面两行可以替换为下面两行,比如进入到C盘,上面地址输入F:	est
eport和F:/test/report,都可以到F:	est
eport这个目录
    # new_file = get_new_file(".\report\")
    # send_mail(new_file)

  

原文地址:https://www.cnblogs.com/uncleyong/p/11711568.html