python3+selenium3发送最新报告

 1、报告名称加时间戳:引入datetime模块

2、获取目录下的所有文件及文件夹

3、文件及文件夹按时间排序

4、用join连接最新报告路径

参考代码:

import unittest
import HTMLTestRunner
import yagmail
import os
import datetime
username = '123456789@qq.com'
passwd = 'dfgdfg'                 # 配置邮箱时生成的密码,不是邮箱密码
smtp = yagmail.SMTP(user=username,
                    password=passwd,
                    host='smtp.qq.com',    # 其他服务器就smtp.126.com
                    smtp_ssl=True
                    )

case_path = os.path.join(os.getcwd(),"case")  # 用例路径
report_path = r"D:\test02\report"            # 报告存放路径
lists = os.listdir(report_path)                 # 获取目录下的所有文件及文件夹
lists.sort(key=lambda fn:os.path.getmtime(report_path+"\"+fn))   # 按时间排序
file = os.path.join(report_path ,lists[-1])          # join连接路径
def all_case():
    discover = unittest.defaultTestLoader.discover(case_path,
                                               pattern="test*.py",
                                               top_level_dir = None)
    print(discover)
    return discover
if __name__ =="__main__":
    # runner = unittest.TextTestRunner()
    # runner.run(all_case())
    # html报告文件路径
    str_time = datetime.datetime.now()
    time_array = str_time.strftime('%Y%m%d%H%M%S')
    file_name = 'result'+ time_array + '.html'
    report_abspath = os.path.join(report_path,file_name)
    fp = open(report_abspath,"wb")
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp,
                                           title = u"自动化测试报告,测试结果如下:",
                                           description = u"用例执行情况:")

    # run所有用例
    runner.run(all_case())
    smtp.send(
        to = '123456789@qq.com',
        subject ='发送邮件的标题',
        contents = '测试用例报告',
        attachments = file                            # 发送最新报告
    )
    print('发送成功')
    fp.close()
原文地址:https://www.cnblogs.com/yuer02/p/12712169.html