1. 程式人生 > 其它 >TEP自動化測試工具

TEP自動化測試工具

tep測試工具簡介

tep的定位是工具也是一種理念而不是框架,因為它是基於pytest框架,tep的作用:

1.使目錄更加規範化

files: 存放指令碼依賴的圖片,資料,音訊之類的檔案

fixture: 存放用例所需要的一些公共方法

reports: 存放allure測試報告

tests:存放測試用例

conf.yaml: 配置檔案,一般應用當前執行用例環境的區分

conftest.py:把fixture, 在conftest檔案中進行封裝,conftest 會遍歷 fixtures 目錄下面所有以fixture_ 開頭 .py 結尾的檔案.,把裡面的 fixture進行自動匯入

pytest.ini: pytest配置

2. 搭建方便

tep已經收錄到了pypi,使得tep擁有了可以快速的專案的能力,也就是腳手架,命令列 執行pip install tep 進行安裝

執行tep startproject project_name,就可以建立專案結構

3. 公共方法儲存簡潔

fixtures公用方法詳情:

fixture_admin.py: 管理員所需要維護的一些公用方法

fixture_env_var.py: 存放全域性環境變數,根據conf.yaml 內填寫的環境 來進行判斷拼接 需要請求的url,或用於其他的環境變數

fixture_login.py: 登入介面

fixture_your_name.py: 專案成員共享的一些fixture

簡潔的介面自動化執行示例:

在tests目錄下進行編寫用例檔案,介面用例運用的yaml進行儲存,好處就是在於yaml檔案支援陣列,配置有序,簡潔

yaml 一個介面的用例結構:

-
name: 當前的用例名稱
case_id: 1
request:
method: post # 介面請求方法
url: /*****/****/ # 介面名
headers: # 請求頭
Content-Type: application/json
authorization: Bearer 7209b501-0680-4a3e-be70-c20f0b64c941
Cache-Control: no-cache
User-AgentL: PostmanRuntime/7.26.8


params: # 請求引數
type: P
department_id: []
approve_start_date: "2021-06-10"

check: # 需要校驗的資料
expected_result: test_all_examine_check.json # 由於需要校驗介面響應的資料過大 可以單獨建立一個json檔案進行儲存

用例完成後,可以再寫一個檔案的取的公共方法

# 查詢yaml檔案 並返回
def read_yaml(yaml_name):
'''
讀取yaml,將yaml反序列化,就是把yaml格式轉換成dict格式
:return:
'''

try:
yaml_file = "../tests/test_case/" + yaml_name
with open(yaml_file, encoding="utf-8") as f:
value = yaml.load(f, Loader=yaml.FullLoader) # 檔案流,載入方式

return value

except Exception as e:
yaml_file = "./tests/test_case/" + yaml_name
with open(yaml_file, encoding="utf-8") as f:
value = yaml.load(f, Loader=yaml.FullLoader) # 檔案流,載入方式

return value

介面用例編寫:

import pytest
from fixtures.fixture_your_name import read_yaml, ys, cmp
from tep.client import request
from loguru import logger
import time
import decimal
import json
import allure
@allure.description("""簡介:此執行集只做除錯使用""")
class Test_Examine(object):

yaml_data = read_yaml("test_all_examine.yaml") # 獲取測試集檔案內容

@allure.description_html("""
<h1>除錯版本</h1>
""")
@pytest.mark.flaky(reruns=3, reruns_delay=3) # 失敗重試 每3秒重試一次,共重試3次
@pytest.mark.parametrize('yaml_case', yaml_data) # 資料驅動,讀取用例執行集yaml檔案中的列表,
@allure.title('稽核模組')
def test_examine(self, yaml_case, env_vars, config):

api = ys(yaml_case, "url")[0] # ys 只是封裝了一個 jsonpath 查詢的公共方法
method = ys(yaml_case, "method")[0]
params = ys(yaml_case, "params")[0]
apinam = ys(yaml_case, "name")[0] # 介面名稱
headers = ys(yaml_case, "headers")[0]
url = env_vars.domain + api # 完整url

logger.info("當前請求用例:%s,URL:%s,請求方法:%s,params:%s,headers:%s"%(apinam, url, method, params, headers))

# 起始時間
start = time.process_time()

# 介面請求
response = request(method, url=url, headers=headers, json=params)

# 請求結束時間
end = time.process_time()

# 介面耗時
elapsed = str(decimal.Decimal("%.3f" % float(end - start))) + "s"
logger.info(' 介面:%s,*************** 執行耗時:%s ***************' % (url, elapsed))

ret = response.json() # 獲取介面json 型別響應:ret

用例執行pytest--tep-reports執行所有用例 或pytest -檔名--tep-reports 執行一個執行集

執行過後可在根目錄生成測試報告目錄 reports 以及allure測試報告

一個簡單的介面測試就完成了~