• python生成自动化测试报告并发送到指定邮箱


    #-*-coding:utf-8 -*-
    import HTMLTestRunner
    import unittest
    import time
    import sys
    import os
    import smtplib
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    from email.header import Header
    from login import umeiLogin
    reload(sys)
    sys.setdefaultencoding('utf8')
    
    
    def new_file(test_dir):
        #列举test_dir目录下的所有文件,结果以列表形式返回。
        lists=os.listdir(test_dir)
        #sort按key的关键字进行排序,lambda的入参fn为lists列表的元素,获取文件的最后修改时间
        #最后对lists元素,按文件修改时间大小从小到大排序。
        lists.sort(key=lambda fn:os.path.getmtime(test_dir+'\\'+fn))
        #获取最新文件的绝对路径
        file_path=os.path.join(test_dir,lists[-1])
        return file_path
    
    
    # 3.定义:发送邮件,发送最新测试报告html
    def send_email(newfile):
        # 打开文件
        f = open(newfile, 'rb')
        # 读取文件内容
        mail_body = f.read()
        # 关闭文件
        f.close()
    
        # 发送邮箱服务器
        smtpserver = 'smtp.163.com'
        # 发送邮箱用户名/密码
        user = 'XXXX@163.com'
        password = 'XXXXX'
        # 发送邮箱
        sender = 'XXXXX@163.com'
        # 多个接收邮箱,单个收件人的话,直接是receiver='XXX@163.com'
        receiver = ['XXXXX@163.com']
        # 发送邮件主题
        subject = '自动化测试报告'
    
        msg = MIMEMultipart('mixed')
    
        msg_html1 = MIMEText(mail_body, 'html', 'utf-8')
        msg.attach(msg_html1)
    
        msg_html = MIMEText(mail_body, 'html', 'utf-8')
        msg_html["Content-Disposition"] = 'attachment; filename="TestReport.html"'
        msg.attach(msg_html)
    
    
    
        # 要加上msg['From']这句话,否则会报554的错误。
        # 要在163设置授权码(即客户端的密码),否则会报535
        msg['From'] = 'XXXX@163.com'
        #    msg['To'] = 'XXX@doov.com.cn'
        # 多个收件人
        msg['To'] = ";".join(receiver)
        msg['Subject'] = Header(subject, 'utf-8')
    
        # 连接发送邮件
        smtp = smtplib.SMTP()
        smtp.connect(smtpserver, 25)
        smtp.login(user, password)
        smtp.sendmail(sender, receiver, msg.as_string())
        smtp.quit()
    
    if __name__=="__main__":
        print '===== Start  ======'
        # 1.执行测试用例,生成最新的测试用例
        # 指定测试用例为当前文件夹下的test_case目录
    
        test_dir = 'D:\\study\\TestCase'
        # 指定测试报告的路径
        test_report_dir = 'D:\\study\\report'
    
        # 包装测试用例
        suite = unittest.TestSuite()
        suite.addTest(unittest.makeSuite(umeiLogin.umeiLogin))
    
        #生成与执行时间相同的测试报告
        now = time.strftime('%Y-%m-%d_%H_%M_%S_')
        filename = test_report_dir + '\\' + now + 'result.html'
        fp = open(filename, 'wb')
    
        runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'测试报告', description=u'用例执行情况:')
        runner.run(suite)
        fp.close()
    
        # 2.取最新测试报告
        new_report = new_file(test_report_dir)
    
        # 3.发送邮件,发送最新测试报告html
        send_email(new_report)
        print '=====  Over   ======'
  • 相关阅读:
    ios开发系列-准备工作
    tests
    腾讯DBA官方博客开通了,欢迎交流
    腾讯DBA官方博客开通了
    [HNOI2008]水平可见直线
    BZOJ-4518 征途
    CDQ分治与整体二分
    HYSBZ-1176 Mokia
    二逼平衡树
    可持久化数组
  • 原文地址:https://www.cnblogs.com/handaxing/p/6952293.html
Copyright © 2020-2023  润新知