zoukankan      html  css  js  c++  java
  • 完整的发邮件并且生成测试报告的例子

    #coding=utf-8
    import smtplib
    from email.mime.text import MIMEText
    import unittest
    import HTMLTestRunner
    import time,os
    #=============定义发送邮件==========
    def send_mail(file_new):
    #发信邮箱
    mail_from='testingwtb@126.com'
    #收信邮箱
    mail_to='xiaoming@126.com'
    #定义正文
    f = open(file_new, 'rb')
    mail_body = f.read()
    f.close()
    msg=MIMEText(mail_body,_subtype='html',_charset='utf-8')
    #定义标题
    msg['Subject']=u"自动化测试报告"
    #定义发送时间(不定义的可能有的邮件客户端会不显示发送时间)
    msg['date']=time.strftime('%a, %d %b %Y %H:%M:%S %z')
    smtp=smtplib.SMTP()
    #连接 SMTP 服务器,此处用的 126 的 SMTP 服务器
    smtp.connect('smtp.126.com')
    #用户名密码
    smtp.login('testingwtb@126.com','123456')
    smtp.sendmail(mail_from,mail_to,msg.as_string())
    smtp.quit()
    print 'email has send out !'
    #======查找测试报告目录,找到最新生成的测试报告文件====
    def send_report(testreport):
    result_dir = testreport
    lists=os.listdir(result_dir)
    lists.sort(key=lambda fn: os.path.getmtime(result_dir+"\"+fn))
    #print (u'最新测试生成的报告: '+lists[-1])
    #找到最新生成的文件
    file_new = os.path.join(result_dir,lists[-1])
    print file_new
    #调用发邮件模块
    send_mail(file_new)
    #================将用例添加到测试套件===========
    def creatsuite():
    testunit=unittest.TestSuite()
    #定义测试文件查找的目录
    test_dir='.\test_case'
    #定义 discover 方法的参数
    discover=unittest.defaultTestLoader.discove(test_dir,pattern='test*.py',top_level_dir=None)
    #discover 方法筛选出来的用例,循环添加到测试套件中
    for test_case in discover:
    print test_case
    testunit.addTests(test_case)
    return testunit
    if __name__ == '__main__':
    now = time.strftime("%Y-%m-%d %H_%M_%S")
    testreport = 'E:\test_object\report\'
    filename = testreport+now+'result.html'
    fp = file(filename, 'wb')
    runner =HTMLTestRunner.HTMLTestRunner(
    stream=fp,
    title=u'自动化测试报告',
    description=u'用例执行情况:')
    alltestnames = creatsuite()
    runner.run(alltestnames)
    fp.close() #关闭生成的报告
    send_report(testreport) #发送报告

  • 相关阅读:
    vue使用elementui合并table
    使用layui框架导出table表为excel
    vue使用elementui框架,导出table表格为excel格式
    前台传数据给后台的几种方式
    uni.app图片同比例缩放
    我的博客
    【C语言】取16进制的每一位
    SharePoint Solution 是如何部署的呢 ???
    无效的数据被用来用作更新列表项 Invalid data has been used to update the list item. The field you are trying to update may be read only.
    SharePoint 判断用户在文件夹上是否有权限的方法
  • 原文地址:https://www.cnblogs.com/czb529514/p/7676658.html
Copyright © 2011-2022 走看看