zoukankan      html  css  js  c++  java
  • 以邮件附件的形式发送测试报告

    1. 创建 EmailAnnex目录, 在 EmailAnnex 下创建 bing.py,并编写

    from selenium import webdriver
    from time import sleep
    import unittest
    class Bing(unittest.TestCase):
       """bing 搜索测试"""
     def setUp(self):
       self.driver = webdriver.Firefox()
       self.driver.implicitly_wait(10)
       self.base_url = "http://cn.bing.com/"
     def test_bing_search(self):
       driver = self.driver
       driver.get(self.base_url)
       driver.find_element_by_xpath("//input[@id='sb_form_q']").send_keys("CMBC")
       sleep(3)
       driver.find_element_by_xpath("//input[@id='sb_form_go']").click()
     def tearDown(self):
     self.driver.quit()

    2.在 EmailAnnex 创建 send_mail.py 并编写

    from HTMLTestRunner import HTMLTestRunner
    from email.mime.text import MIMEText
    from email.header import Header
    from email.mime.multipart import MIMEMultipart
    import smtplib
    import unittest
    import time
    import os
    # ===================发送邮件=============================
    def sendReport(file_path):
       """发送带附件的邮件"""
       sendfile = open(file_path,"rb").read() #读取测试报告路径
       # 如下几行是为了以附件的形式,发送邮件
       msg = MIMEText(sendfile,"base64","utf-8")
       msg["Content-Type"] = "application/octet-stream"
       msg["content-Disposition"] = "attachment;filename=result.html"
      #result.html 邮件附件的名字
       msgRoot = MIMEMultipart("related")
       msgRoot.attach(msg)
       msg['Subject'] = Header("自动化测试报告","utf-8")
       msg['From'] = "hblxp4321@126.com" #发送地址
       msg['To'] = "hblxp4321@126.com" #收件人地址,如果是多个的话,以分号隔开
       smtp = smtplib.SMTP('smtp.126.com')
       smtp.login("hblxp4321@126.com","Abcd123") #邮箱的账户和密码
       smtp.sendmail(msg['From'],msg['To'].split(';'),msg.as_string())
       smtp.quit()
       print("Test Result has send out!!!")
    # =================查找测试报告目录,找到最新的测试报告文件========
    def newReport(testReport):
       lists = os.listdir(testReport) #返回测试报告所在的目录下所有文件夹
       lists2 = sorted(lists) # 获得升序排列后端测试报告列表
       file_new = os.path.join(testReport,lists2[-1]) #获得最新一条测试报告的地址
       print(file_new)
       return file_new
    # ==================运行===================================
    if __name__ == '__main__':
       test_dir = "D:\python\autotest\EmailAnnex" #测试用例所在的目录
       test_report = "D:\python\autotest\EmailAnnex\result" #测试报告所在目录
       discover =unittest.defaultTestLoader.discover(test_dir,pattern="bing.py")
       now = time.strftime("%Y-%m-%d %H%M%S") #获取当前时间
       filename = test_report + '\' + now + 'result.html' #拼接出测试报告名
       fp = open(filename,"wb")
       runner = HTMLTestRunner(stream=fp,title="测试报告",description="测试用例执行情况")
       runner.run(discover)
       fp.close()
       new_report = newReport(test_report) #获取最新的测试报告
       print(new_report)
       sendReport(new_report) #发送测试报告邮件
    

      

  • 相关阅读:
    MSMQ简例
    C#观察者模式简例
    C#常见算法题目(面试准备)
    HttpWebRequest
    自定义Attribute简例
    .Net下的 ORM框架介紹
    for xml path的应用
    .net中日至框架log4net.dll如何使用
    动态载入.ascx用户控件
    wap 2.0 编写规范
  • 原文地址:https://www.cnblogs.com/yangyang521/p/10077099.html
Copyright © 2011-2022 走看看