python+selenium+HTMLTestRunner生成测试报告并发送邮件给指定邮箱

配置邮件发送人及接收人,并执行测试用例,最后发送测试报告到指定邮箱(注意:打开测试报告目录后,最后记得关闭,不然邮箱接收到的测试报告内容将会为空)

import time
import unittest
import os
import smtplib
from HTMLTestRunner import HTMLTestRunner
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart

#================发送邮件===================
def send_mail(file_new):
    mail_host = 'smtp.qq.com'
    mail_user = '123456@qq.com'
    mail_pwd = 'v*****g'  # qq生成的授权码

    receivers = ['123456@126.com']
    subject = '自动化测试报告'

    f=open(file_new,'rb')
    mail_content=f.read()

    #把报告作为邮件内容
    #msg=MIMEText(mail_content,'html','utf-8')
    #msg['Subject']=Header(subject,'utf-8')

    #把报告作为附件发送
    att = MIMEText(mail_content, 'base64', 'utf-8')
    att['Content-Type'] = 'application/octet-stream'
    att['Content-Disposition'] = 'attachment; filename=test_report.html'

    msg = MIMEMultipart('related')
    msg['Subject'] = Header(subject,'utf-8')
    msg.attach(att)

    #连接邮箱,登录,发送邮件
    smtpObj = smtplib.SMTP()
    smtpObj.connect(mail_host)
    # 上面两行也可以写成:smtpObj=smtplib.SMTP_SSL(mail_host,465)
    smtpObj.login(mail_user, mail_pwd)
    smtpObj.sendmail(mail_user, receivers, msg.as_string())

#=====================查找最新的测试报告===================
def new_report(test_report):
    lists = os.listdir(test_report)
    # lambda argument_list: expression表示的是一个函数
    # 比如:lambda x, y: x*y;函数输入是x和y,输出是它们的积x*y
    # lists.sort(key=lambda fn: os.path.getmtime(result_dir+'\\'+fn))
    lists.sort(key=lambda fn: os.path.getmtime(test_report + '/' + fn))
    latest_file = os.path.join(test_report, lists[-1])
    print(latest_file)
    return latest_file

def main():
    """
    一个一个加
    suite = unittest.TestSuite()
    # suite.addTest(TestAdd("test_case"))
    suite.addTest(TestAdd("test_add1"))
    suite.addTest(TestAdd("test_add2"))
    suite.addTest(TestSub("test_sub1"))
    suite.addTest(TestSub("test_sub2"))
    runner = unittest.TextTestRunner()
    runner.run(suite)

    """

    test_dir = r"./test_case/"
    test_report=r'./report/'

    """
    TestLoader是用来加载TestCase到TestSuite中的,
    其中有几个loadTestsFrom__()方法,就是从各个地方寻找TestCase,
    创建它们的实例,然后add到TestSuite中,再返回一个TestSuite实例
    """
    discover = unittest.defaultTestLoader.discover(test_dir, pattern="test_*.py")

    #给生成的测试报告设置名称
    current_time = time.strftime("%Y-%m-%d %H_%M_%S")
    file_name=test_report+current_time+"_result.html"

    fp=open(file_name,"wb")
    runner = HTMLTestRunner(stream=fp,
                            title="测试报告",
                            description="用例执行情况啦啦啦:")
    runner.run(discover)
    fp.close()
    
    latest_report=new_report(test_report)
    send_mail(latest_report)

if __name__ == '__main__':
    main()

百度为例如下:

from selenium import webdriver
import unittest
import time
from HTMLTestRunner import HTMLTestRunner

class Baidu(unittest.TestCase):
    def setUp(self):
        self.driver=webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)
        self.base_url="http://www.baidu.com"

    def test_baidu_search(self):
        '''百度搜索测试一下注释'''
        driver=self.driver
        driver.get(self.base_url)
        driver.find_element_by_id("kw").clear()
        driver.find_element_by_id("kw").send_keys("HTMLTestRunner")
        driver.find_element_by_id("su").click()
        time.sleep(2)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    """
    testunit=unittest.TestSuite()
    testunit.addTest(Baidu("test_baidu_search"))
    current_time=time.strftime("%Y-%m-%d %H_%M_%S")
    file_name="C:\\Users\\xxx\\Documents\\"
                +current_time+"_result.html"
    fp=open(file_name,'wb')

    runner=HTMLTestRunner(stream=fp,
                          title="百度搜索测试报告",
                          description="用例执行情况:")
    runner.run(testunit)
    fp.close()
    """
    unittest.main()

相关推荐

  1. Golang- 邮件服务,发送邮件

    2023-12-20 16:40:04       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-20 16:40:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-20 16:40:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-20 16:40:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-20 16:40:04       18 阅读

热门阅读

  1. html css背景图片透明文字不透明

    2023-12-20 16:40:04       36 阅读
  2. vuex--未完

    2023-12-20 16:40:04       48 阅读
  3. Web安全 - 深入同源策略

    2023-12-20 16:40:04       38 阅读
  4. 蓝底白字车牌的定位与字符分割识别 MATLAB 仿真

    2023-12-20 16:40:04       34 阅读
  5. [PTA]矩阵列平移

    2023-12-20 16:40:04       38 阅读
  6. 05-MySQL中的limit和union关键字

    2023-12-20 16:40:04       33 阅读
  7. 【libevent】IO引擎及实现

    2023-12-20 16:40:04       30 阅读
  8. springboot 解析微信小程序获取手机号

    2023-12-20 16:40:04       23 阅读
  9. 基于改进鲸鱼算法的最小乘支持向量机数据分类

    2023-12-20 16:40:04       29 阅读
  10. Python用Pygame实现一个五子棋小游戏

    2023-12-20 16:40:04       35 阅读
  11. iOS将framework转为xcframework

    2023-12-20 16:40:04       35 阅读
  12. Dubbo RPC-Redis协议

    2023-12-20 16:40:04       42 阅读
  13. Prolist组件实现动态竖排展示

    2023-12-20 16:40:04       33 阅读
  14. 在vue中,文件转base64示例

    2023-12-20 16:40:04       33 阅读
  15. WPF 全局异常处理

    2023-12-20 16:40:04       40 阅读
  16. Spring 声明式事务

    2023-12-20 16:40:04       41 阅读