1. 程式人生 > 其它 >Pyhton3+AirTest+[002]+小程式UI自動化之Unittest+AirTest融合框架介紹

Pyhton3+AirTest+[002]+小程式UI自動化之Unittest+AirTest融合框架介紹

一、目的

  1. 使用unittest的目的是為了批量執行測試用例

二、框架

  1. 目錄
common:放一些公共類,例如:HTMLTestRunner等 conf:放一些資料庫,固定的設定和元素定位路徑等 data:放一些測試資料 lib:同common loctor:讀取conf/setting下的內容封裝 page:拿到loctor中的內容 page_element:封裝一些頁面元素的定位等操作 log:日誌 report:測試html報告 testCase:測試用例目錄 utils:工具類 caseList:測試用例執行順序檔案設定 runCase:主入口
  1. 主入口
#!/usr/bin/python3.8
# -*- coding: utf-8 -*- # @Time : 2021/7/17 10:48 # @Author : csjin # @File : runCase.py import unittest from common import HTMLTestRunner_cn from conf.settings import * #定義一個類AllTest class AllTest(): # 初始化一些引數和資料 def __init__(self): global resultPath # result/report.html
resultPath = os.path.join(report_path, "report.html") # 配置執行哪些測試檔案的配置檔案路徑 self.caseListFile = os.path.join(root_path, "caseList.txt") # 真正的測試斷言檔案路徑 self.caseFile = os.path.join(root_path, "testCase") self.caseList = [] def set_case_list(self):
""" 讀取caselist.txt檔案中的用例名稱,並新增到caselist元素組 :return: """ fb = open(self.caseListFile,encoding="utf-8") for value in fb.readlines(): data = str(value) if data != '' and not data.startswith("#"): # 如果data非空且不以#開頭 self.caseList.append(data.replace("\n", "")) # 讀取每行資料會將換行轉換為\n,去掉每行資料中的\n fb.close() print(self.caseList) def set_case_suite(self): """ :return: """ # 通過set_case_list()拿到caselist元素組 self.set_case_list() # 建立測試套件 test_suite = unittest.TestSuite() suite_module = [] # 從caselist元素組中迴圈取出case for case in self.caseList: # 通過split函式來將aaa/bbb分割字串,-1取後面,0取前面 case_name = case.split("/")[-1] # 打印出取出來的名稱 # print(case_name + ".py") # 批量載入用例,第一個引數為用例存放路徑,第二個引數為規則 discover = unittest.defaultTestLoader.discover(self.caseFile, pattern=case_name + '.py',top_level_dir=None) # 將discover存入suite_module元素組 suite_module.append(discover) # print('suite_module:'+str(suite_module)) # 判斷suite_module元素組是否存在元素 if len(suite_module) > 0: # 如果存在,迴圈取出元素組內容,命名為suite for suite in suite_module: # 從discover中取出test_name,使用addTest新增到測試集 for test_name in suite: test_suite.addTest(test_name) else: print('測試套件中無可執行的測試用例') return None return test_suite def run(self): """ run test :return: """ print("*********TEST START*********") try: # 呼叫set_case_suite獲取test_suite suit = self.set_case_suite() # 判斷test_suite是否為空 if suit is not None: # 開啟result/report.html測試報告檔案,如果不存在就建立 fp = open(resultPath, 'wb') # 呼叫HTMLTestRunner runner = HTMLTestRunner_cn.HTMLTestRunner(stream=fp, title=start_mini_app+'小程式測試報告', description='測試用例執行結果') # 通過HTMLTestRunner的run()方法來執行測試套件中的測試用例,並寫入測試報告 runner.run(suit) else: print("Have no appCommon to test.") except Exception as ex: print(str(ex)) finally: print("\n"+"*********TEST END*********") fp.close() # # 判斷郵件傳送的開關 # if on_off == 'on': # send_mail.send_Mail(mail_path, "Sales Interface Test") # else: # print("郵件傳送開關配置關閉,請開啟開關後可正常自動傳送測試報告") if __name__ == '__main__': AllTest().run()
  1. caseList用例執行策略
#這裡存放要執行的case,帶#號的測試用例不會執行
 
# 開啟小程式
test_init_start_app/test_init_start_app
當有些人一出生就有的東西,我們要為之奮鬥幾十年才擁有。但有一樣東西,你一輩子都不會有,那就是我們曾經一無所有。