• Python Selenium unittest+HTMLTestRunner实现 自动化测试及发送测试报告邮件


      

     1、UI测试框架搭建-目录结构

     2、 文件介绍

    2.1、baseinfo->__init__.py

    配置文件定义基础参数

    #-*-coding:utf-8-*-
    #测试用例配置参数
    
    base_url = "http://xxxxx.com"
    undirect_username = "username"
    undirect_password = "password"
    direct_username = "username"
    direct_password = "password"
    
    buyerName = 'username'
    buyerPassword = 'password'
    
    sellerName = 'username'
    sellerPassword = 'password'
    
    
    #发送邮件配置参数
    Smtp_Server = 'smtp.xxxx.com'
    Smtp_Sender = 'nnv@xx.cn'
    Smtp_Sender_Password = 'Password'
    #Smtp_Receiver = ['nnv1@xx.cn','nnv2@xx.cn','nnv3@xx.cn','nnv4@xx.cn']
    Smtp_Receiver = ['nnv1@xx.cn']
    Smtp_Receiver_pre = ['nnv1@xx.cn']
    
    
    #测试邮箱配置
    #Smtp_Server = 'smtp.163.com'
    #Smtp_Sender = 'wmqyyj_test@163.com'
    #Smtp_Sender_Password = 'Password'
    #Smtp_Receiver = ['wmqyyj_test@163.com']
    #Smtp_Receiver_pre = ['wmqyyj_test@163.com']
    
    
    #测试用例及报告路径配置参数
    # case_dir = './test_case'
    # test_report = './test_report/'
    
    #测试用例及报告路径配置参数
    test_dir = 'D:\WorkSpace\Python\UiTest\test_case'
    test_report = 'D:\WorkSpace\Python\UiTest\test_report\'

    2.2、caseData

    用于存放测试数据,这里用于存放文件上传功能中的上传的文件。

    2.3、test_case

    用于存放测试用例,根据下图定义的pattern的值,所以用例必须以test开头

    2.4、test_report

    用户存放测试报告文件

     报告内容如下

     

     2.5、测试执行程序 

    编写用例执行程序 runtest.py

    # -*-coding:utf-8-*-
    # Time:2017/7/1-0:18
    # Author:YangYangJun
    import sys
    
    reload(sys)
    sys.setdefaultencoding('utf8')
    import smtplib
    import unittest
    import time
    import os
    from HTMLTestRunner import HTMLTestRunner
    from email.header import Header
    from email.mime.text import MIMEText
    import baseinfo from email.mime.multipart import MIMEMultipart from selenium import webdriver def get_NewReport(testreport): #获取testreport 目录下的文件返回一个list dirs = os.listdir(testreport) #对文件list 进行排序 进行增序排列 dirs.sort() #获取序列最后一个元素,即最大的一个元素。 newreportname = dirs[-1] print('The new report name: {0}'.format(newreportname)) file_new = os.path.join(testreport, newreportname) print file_new return file_new def get_Result(filename): driver = webdriver.Firefox() driver.maximize_window() ##得到测试报告路径 result_url = "file://%s" % filename driver.get(result_url) time.sleep(5) result = driver.find_element_by_xpath("html/body/div[1]/p[3]").text result = result.split(':') print result driver.quit() return result[-1] def send_Mail(file_new,result): f = open(file_new, 'rb') # 读取测试报告正文 mail_body = f.read() f.close() try: smtp = smtplib.SMTP(baseinfo.Smtp_Server, 25) sender = baseinfo.Smtp_Sender password = baseinfo.Smtp_Sender_Password receiver = baseinfo.Smtp_Receiver smtp.login(sender, password) msg = MIMEMultipart() # 编写html类型的邮件正文,MIMEtext()用于定义邮件正文 # 发送正文 text = MIMEText(mail_body, 'html', 'utf-8') # 定义邮件正文标题 text['Subject'] = Header('XXXXUI自动化测试报告', 'utf-8') msg.attach(text) # 发送附件 # Header()用于定义邮件主题,主题加上时间,是为了防止主题重复,主题重复,发送太过频繁,邮件会发送不出去。 msg['Subject'] = Header('[执行结果:' + result + ']'+ 'XXXXUI自动化测试报告' + now, 'utf-8') msg_file = MIMEText(mail_body, 'html', 'utf-8') msg_file['Content-Type'] = 'application/octet-stream' msg_file["Content-Disposition"] = 'attachment; filename="TestReport.html"' msg.attach(msg_file) # 定义发件人,如果不写,发件人为空 msg['From'] = sender # 定义收件人,如果不写,收件人为空 msg['To'] = ",".join(receiver) tmp = smtp.sendmail(sender, receiver, msg.as_string()) print tmp smtp.quit() return True except smtplib.SMTPException as e: print(str(e)) return False if __name__ == '__main__': # test_dir = os.path.join(os.getcwd(),'test_case') # print(test_dir) # report_dir = os.path.join(os.getcwd(),'test_report') # 测试用例路径 test_dir = baseinfo.test_dir # est_dir = os.path.join(os.getcwd(),'test_case') # print(test_dir) # report_dir = os.path.join(os.getcwd(),'test_report') # 测试报告存放路径 report_dir = baseinfo.test_report test_discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py') now = time.strftime("%Y-%m-%d-%H_%M_%S") filename = report_dir + 'result-' + now + '.html' print filename fp = open(filename, 'wb') runner = HTMLTestRunner(stream=fp, title='XXXXUI自动化测试报告', description='用例执行情况') runner.run(test_discover) fp.close() #其实根本不需要这里获取最新报告,filename就是最新的报告了,可以直接使用了 #new_report = get_NewReport(report_dir) result = get_Result(filename) mail = send_Mail(filename,result) print mail if mail: print(u"邮件发送成功!") else: print(u"邮件发送失败!")
  • 相关阅读:
    【华为云技术分享】从自建MongoDB聊聊云数据库MongoDB的蓬勃张力
    【华为云技术分享】【Python算法】分类与预测——支持向量机
    Python 中更优雅的环境变量设置方案
    Python解析照片EXIF信息,获取坐标位置
    【华为云技术分享】【Python算法】分类与预测——决策树
    【华为云技术分享】使用keil5打开GD32F450i的MDK项目出现的问题以及J-Link无法烧录程序对应的解决方案
    【华为云技术分享】小熊派IoT开发板华为物联网操作系统LiteOS内核实战教程01-IoT-Studio介绍及安装
    【华为云技术分享】【开发记录】Linux服务器维护常用命令(二)
    【PHP输出两位小数】使用PHP来输出保留两位小数的数字【原创】
    【SERVER_NAME】PHP中的SERVER_NAME【原创】
  • 原文地址:https://www.cnblogs.com/BlueSkyyj/p/7527668.html
Copyright © 2020-2023  润新知