• Python+selenium+HTMLTestRunner邮件形式发送测试报告


    已邮件的形式发送测试报告:

      以打开bing主页,搜索“墨菲特”这样一个动作作为测试用例,然后将测试的结果以邮件的方式发送到126邮箱(自发自收)

    1.编写测试用例Demo(bing.py)

    from selenium import webdriver
    from time import sleep
    import unittest
    class Bing(unittest.TestCase):
        # 搭建测试环境
        def setUp(self):
            self.driver = webdriver.Chrome()
            self.driver.implicitly_wait(10)
            self.base_url = "https://cn.bing.com/"
        # 测试步骤
        def test_bingSearch(self):
            driver = self.driver
            driver.get(self.base_url)
            driver.find_element_by_xpath('//*[@id="sb_form_q"]').send_keys("墨菲特")
            sleep(3)
            driver.find_element_by_xpath('//*[@id="sb_form_go"]').click()
        # 还原环境
        def tearDown(self):
            self.driver.quit()

      这就是测试用例的Demo,可以先运行一下试试能否正确的执行

    2.编写发送邮件代码send_mail.py

    from HTMLTestRunner import HTMLTestRunner
    from email.mime.text import MIMEText
    from email.header import Header
    import smtplib
    import unittest
    import os
    import time
    
    # 发送邮件
    def sendReport(file_new):
        # 以只读的方式打开文件,取别名为f
        with open(file_new,"rb") as f:
            # 读取文件内容,存放到mai_body中
            mail_body = f.read()
        # 发邮件的内容
        msg = MIMEText(mail_body,"html","utf-8")
        # 标题
        msg["Subject"] = Header("自动化测试报告",'utf-8')
        # 发件人
        msg["From"] = "用户名"
        # 收件人,如果给多人发送邮件,以分号隔开
        msg["To"] = "用户名"
        # SMTP协议,调用smtp.qq.com
        smtp = smtplib.SMTP('smtp.126.com')
        # 登陆邮箱,参数为用户名和授权码
        smtp.login("用户名", "授权码")
        # 发件人给接收者发送邮件,收件人以分号隔开,内容以字符串形式
        smtp.sendmail(msg["From"],msg["To"].split(";"),msg.as_string())
        # 退出
        smtp.quit()
    
    # 查找测试报告目录,找到最新的测试报告文件,把最新的测试报告文件作为邮件发送内容
    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:\PythonDemo\SeleniumProject\Email"
        test_report = "D:\testresult"
        # 加载测试用例所在目录下的bing.py测试用例文件
        discover = unittest.defaultTestLoader.discover(test_dir,pattern="bing.py")
        #获取当前时间
        now = time.strftime("%Y-%m-%d %H%M%S")
        # 测试报告名,测试结果路径+时间+result.html
        filename = test_report + '\' + now + 'result.html'
        # 以只读的方式打开测试报告
        fp = open(filename,"wb")
        runner = HTMLTestRunner(stream=fp,title="测试报告",description="测试用例执行情况")
        # 执行测试用例,测试用例在discover中加载
        runner.run(discover)
        fp.close()
    
        # 拿到最新的测试报告
        new_report = newReport(test_report)
        #发送邮件
        sendReport(new_report)

      这里把测试报告放在D:\testresult下,测试用例路径就是存放相应代码的路径。注意这里由于是第三方登录邮箱,所以要获取对应的授权码,具体怎么设置需要根据具体的邮箱设置方法去看。126邮箱设置的方法如下:

      在这个POP3/SMTP/IMAP中去获取授权码,然后复制粘贴到代码中的授权码的地方,就OK了。

  • 相关阅读:
    对 Unity 脚本生命周期的调研
    实现僵尸跑酷游戏的 UGUI 实践
    Unity UGUI 按钮绑定事件的 4 种方式
    virtualbox下给centos7固定ip
    linux新建文件夹
    centos7修改hostname
    linux下杀进程的方法
    virtualbox下最小化安装centos7后上网设置
    ubuntu下自动获取ip设置
    Caused by: org.springframework.beans.NotWritablePropertyException:
  • 原文地址:https://www.cnblogs.com/bigbigtong/p/10274536.html
Copyright © 2020-2023  润新知