python+unittest框架整理(一點點學習前輩們的封裝思路,一點點成長。。。)
阿新 • • 發佈:2017-05-27
spa star 世界 ner bytes odi splay 好玩 etime
預期框架整理目標:
1.單個用例維護在單個.py文件中可單個執行,也可批量生成組件批量執行
2.對定位參數,定位方法,業務功能腳本,用例腳本,用例批量執行腳本,常用常量進行分層獨立,各自維護在單獨的.py文件中
3.加入日誌,htlm報表,發送郵件功能
框架結構
結構說明:
config:配置部分,瀏覽器種類和定位信息維護在此處
constant:常量部分,固定不變的數據維護在此處
data:存放用於參數化的文本表格等文件
encapsulation:定位等selenium功能二次封裝在此處
error_picture:存放錯誤截圖
function:業務功能腳本維護在此處
log:存放log類
report:存放測試報告文件
test_case:存放用例文件
all_case.py:用來執行所有用例
debug_case.py:本人調試用的,可以忽略
tst.log:生成的日誌
逐個介紹各個包下面的.py文件,並附上源碼(說明見註釋哈哈~):
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2017-05-11 13:42 from selenium import webdriver import time from selenium.webdriver.common.action_chains import * #config_01.pyconfig配置部分 # 瀏覽器種類維護在此處 browser_config = { ‘ie‘: webdriver.Ie, ‘chrome‘: webdriver.Chrome } # 定位信息維護在此處,維護結構由外到內為:頁面名稱--頁面下元素名稱--元素的定位方式+參數 locat_config = { ‘博客園首頁‘: { ‘找找看輸入框‘: [‘id‘, ‘zzk_q‘], ‘找找看按鈕‘: [‘xpath‘, ‘//input[@value="找找看"]‘] } }
1 #!/usr/bin/env pythonconstant_1.py2 # -*- coding: utf-8 -*- 3 # @Time : 2017-05-15 13:20 4 5 # 常量部分(固定不變使用頻繁的參數維護在此處) 6 LOGIN_URL = ‘https://www.cnblogs.com/‘
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # @Time : 2017-05-15 13:20 4 5 # 封裝部分維護在此 6 7 from config.config_01 import locat_config 8 from log.log import Logger 9 from selenium.webdriver.support.wait import WebDriverWait 10 11 class UIHandle(): 12 logger = Logger() 13 14 # 構造方法,用來接收selenium的driver對象 15 @classmethod 16 def __init__(cls, driver): 17 cls.driver = driver 18 19 # 輸入地址 20 @classmethod 21 def get(cls, url): 22 cls.logger.loginfo(url) 23 cls.driver.get(url) 24 25 # 關閉瀏覽器驅動 26 @classmethod 27 def quit(cls): 28 cls.driver.quit() 29 30 # element對象(還可加入try,截圖等。。。) 31 @classmethod 32 def element(cls, page, element): 33 # 加入日誌 34 cls.logger.loginfo(page) 35 # 加入隱性等待 36 WebDriverWait(cls.driver, 10) 37 # 此處便可以傳入config_o1中的dict定位參數 38 el = cls.driver.find_element(*locat_config[page][element]) 39 # 加入日誌 40 cls.logger.loginfo(page+‘OK‘) 41 return el 42 # element對象(還未完成。。。) 43 def elements(cls, page, element): 44 # 加入日誌 45 cls.logger.loginfo(page) 46 # 加入隱性等待 47 WebDriverWait(cls.driver, 10) 48 els = cls.driver.find_elements(*locat_config[page][element]) 49 # 註意返回的是list 50 return els 51 52 # send_keys方法 53 @classmethod 54 def Input(cls, page, element, msg): 55 el = cls.element(page, element) 56 el.send_keys(msg) 57 58 # click方法 59 @classmethod 60 def Click(cls, page, element): 61 el = cls.element(page, element) 62 el.click()encapsulation.py
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # @Time : 2017-05-15 13:22 4 5 # 業務功能腳本(用例腳本可調用此處的功能腳本) 6 7 from encapsulation.encapsulation import UIHandle 8 from constant.constant_1 import LOGIN_URL 9 from config.config_01 import browser_config 10 from time import sleep 11 12 # 打開博客園首頁,進行找找看搜索功能 13 def search(msg): 14 # 打開瀏覽器 15 driver = browser_config[‘chrome‘]() 16 # 傳入driver對象 17 uihandle = UIHandle(driver) 18 #輸入url地址 19 uihandle.get(LOGIN_URL) 20 # 調用二次封裝後的方法,此處可見操作了哪個頁面,哪個元素,msg是要插入的值,插入值得操作在另外一個用例文件中傳入 21 uihandle.Input(‘博客園首頁‘, ‘找找看輸入框‘, msg) 22 uihandle.Click(‘博客園首頁‘, ‘找找看按鈕‘) 23 uihandle.quit()function_01.py
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # @Time : 2017-05-17 11:19 4 import logging 5 import logging.handlers 6 7 # 日誌類 8 class Logger(): 9 LOG_FILE = ‘tst.log‘ 10 11 handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes = 1024*1024, backupCount = 5) # 實例化handler 12 fmt = ‘%(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(message)s‘ 13 14 formatter = logging.Formatter(fmt) # 實例化formatter 15 handler.setFormatter(formatter) # 為handler添加formatter 16 17 logger = logging.getLogger(‘tst‘) # 獲取名為tst的logger 18 logger.addHandler(handler) # 為logger添加handler 19 logger.setLevel(logging.DEBUG) 20 def loginfo(self, message): 21 self.logger.info(message) 22 23 def logdebug(self, message): 24 self.logger.debug(message)log.py
1 #!/usr/bin/env python 2 #!/usr/bin/env python 3 # -*- coding: utf-8 -*- 4 # @Time : 2017-05-15 15:30 5 import unittest 6 from function.function_01 import * 7 # 用例 8 class Case_02(unittest.TestCase): 9 u‘‘‘哇塞好玩‘‘‘ 10 def setUp(self): 11 pass 12 13 def test_zzk(self): 14 u‘‘‘輸入哇塞好玩後點擊找找看‘‘‘ 15 search("哇塞好玩") 16 print(‘打印方法名:test_zzk‘) 17 18 def tearDown(self): 19 pass 20 21 if __name__ == "__main__": 22 unittest.main()start_case_01.py
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # @Time : 2017-05-10 16:34 4 import unittest 5 import HTMLTestRunner 6 import time,os,datetime 7 import smtplib 8 from email.mime.text import MIMEText 9 from email.mime.multipart import MIMEMultipart 10 from email.mime.image import MIMEImage 11 12 13 14 # 取test_case文件夾下所有用例文件 15 def creatsuitel(lists): 16 testunit = unittest.TestSuite() 17 # discover 方法定義 18 discover = unittest.defaultTestLoader.discover(lists, pattern=‘start_*.py‘, top_level_dir=None) 19 #discover 方法篩選出來的用例,循環添加到測試套件中 20 for test_suite in discover: 21 for test_case in test_suite: 22 testunit.addTests(test_case) 23 print(testunit) 24 return testunit 25 list_1 = ‘test_case\\test_case_1‘ 26 alltestnames = creatsuitel(list_1) 27 28 #取前面時間加入到測試報告文件名中 29 now = time.strftime("%Y-%m-%M-%H_%M_%S", time.localtime(time.time())) 30 filename = "report\\"+now+‘result.html‘ #定義個報告存放路徑,支持相對路徑。 31 fp = open(filename, ‘wb‘) 32 runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=‘Report_title‘, description=‘Report_description‘) 33 34 if __name__ == "__main__": 35 # 執行測試用例集並生成報告 36 runner = unittest.TextTestRunner()all_case.py
架子比較簡陋,可以跑通,[email protected][email protected]化,歡迎建議,指點,本人QQ:915069792
為什麽選擇?
有的人喜歡創造世界,他們做了程序員
有的人喜歡拯救世界,他們做了測試員
python+unittest框架整理(一點點學習前輩們的封裝思路,一點點成長。。。)