自动化测试框架雏形搭建完成
小结
框架终于差不多完成了,包含了
等模块。
目前可以实现:
1 2 3 4 5
| 测试过程中控制台实时打印日志 测试过程中实时写日志入自定义log文件 测试过程中发生断言失败,实时截图,并且将堆栈日志打印到指定log文件 测试完成自动生成html格式测试报告 测试完成自动发送邮件
|
实例
以下是实现的简单的从excel中读取数据进行自动化测试的实例(测试用例使用excel编写)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @author: kyle @time: 2018/2/9 15:30 """ from selenium import webdriver from ExcelUtil import GetDataFromExcel from selenium.common.exceptions import NoSuchElementException from Log import * from MailSend import MailSend from ReportTemplate import htmlTemplate from nose.tools import assert_true from MakeDirs import * import time,traceback import sys,ddt
# excel路径,sheet名 path = "G:/workstation/py_workstation/DataDriverTest/DdtData/testData.xlsx" sheetname = "search_data" excel = GetDataFromExcel(path, sheetname) @ddt.ddt class TestDdtByExcel():
@classmethod def setUpClass(cls): TestDdtByExcel.trStr = ""
def setUp(self): self.browser = webdriver.Chrome() # 设置测试状态及结果标志 self.status = None self.flag = 0
@ddt.data(* excel.getData()) def test_ddtbyexcel(self, data): # 声明全局变量 global start, starttime # 定义执行结果的颜色 flagDict = {0: 'red', 1: '00AC4E'} # 获取测试用例名称 casename = sys._getframe().f_code.co_name testdata, expectdata = tuple(data) url = "https://www.baidu.com" self.browser.get(url) self.browser.implicitly_wait(10) try: # 获取当前时间戳 start = time.time() # 获取当前时间 starttime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 搜索 self.browser.find_element_by_id('kw').send_keys(testdata) self.browser.find_element_by_id('su').click() # 休眠3s time.sleep(3) # 断言 assert_true(expectdata in self.browser.page_source) except NoSuchElementException as e: error(u"页面元素不存在:" + str(traceback.print_exc( file=open("G:/workstation/py_workstation/DataDriverTest/Logs/Error/DdtTest.log", "a")))) self.flag = 0 self.status = 'fail' takeScreenShot(self.browser) except AssertionError as e: error(u"搜索“{0},期望“{1}”,失败”".format(testdata, expectdata) + str(traceback.print_exc( file=open("G:/workstation/py_workstation/DataDriverTest/Logs/Error/DdtTest.log", "a")))) self.flag = 0 self.status = 'fail' takeScreenShot(self.browser) except Exception as e: error(u"未知错误:" + str(traceback.print_exc( file=open("G:/workstation/py_workstation/DataDriverTest/Logs/Error/DdtTest.log", "a")))) self.flag = 0 self.status = 'fail' takeScreenShot(self.browser) else: info(u"搜索“{0},期望“{1}”,通过”".format(testdata, expectdata)) self.status = 'pass' self.flag = 1 # 计算消耗时间 # 10位时间戳,单位为s spends = time.time() - start - 3 # 取两位小数 spendtime = "%.2f" %spends # 报告中添加数据 TestDdtByExcel.trStr += u''' <tr> <td>{0}</td> <td>{1}</td> <td>{2}</td> <td>{3}</td> <td>{4}</td> <td style="color: {5}">{6}</td> </tr> '''.format(casename, testdata, expectdata, starttime, spendtime, flagDict[self.flag], self.status)
def tearDown(self): self.browser.quit()
@classmethod def tearDownClass(cls): # 写自定义报告 htmlTemplate(TestDdtByExcel.trStr)
if __name__ == '__main__': os.system("nosetests -s -v {0}".format(__file__)) info(u"*****测试报告开始发送*****") report_file = "G:/workstation/py_workstation/DataDriverTest/Report/DDTByExcel.html" mail_subject = "NoseTest测试报告_{0}".format(dt.now().strftime("%Y%m%d")) mailsend = MailSend(mail_subject, report_file) mailsend.sendMail() info(u"~~~测试报告发送完成,请注意查收~~~")
|
结构
粗略结构:
1 2 3 4 5 6 7 8 9 10
| project |—— coding(主要代码) |—— DdtConf(一些配置,包括邮箱(.ini),日志(logger.conf)等) |—— DdtData(测试数据(excel,xml,json)等) |—— DdtTools(工具类(生成html文件,创建文件夹,截图,邮件发送,从文件读数据))等) |—— Logs(日志) |—— Info |—— Error |—— Pictures(断言失败的截图) |—— Report(测试报告)
|
后期再慢慢改进,可以修改的地方还有很多!