pytest介紹
pytest是一個非常成熟的自動化框架,比其他框架更靈活更容易上手
pytest可以和selenium,request,appium結合實作web自動化,介面自動化,app自動化
pytest可以時間testcase的跳過以及是失敗重試
pytest可以和allure生成美觀報告
pytest可以和jenkins持續集成
安裝pytest
安裝python環境,安裝pip,安裝pycham
安裝pytest
在cmd中輸入pip install -U pytest
插件介紹
pytest
pytest-html(生成測驗報告)
pytest-xdist(測驗用例分布式執行,多CPU分發)
pytest-ordering(用于改變測驗用例的執行順序)
pytet-rerunfaileures(用例失敗后重跑)
allure-pytest(生成美觀的測驗報告)
用pip install -U 插件名 安裝以上插件
基礎配置介紹
默認規則:
模塊名必須以test_開頭或者_test結尾
測驗類必須以Test開頭,并且不能有int方法
測驗方法必須以test開頭
組態檔:
pytest.ini
[pytest]
#--html ./report/report.html #生成html報告到./report目錄
addopts = -vs --alluredir ./temp
#運行加引數 -vs顯示詳細資訊,列印除錯資訊
#--alluredir ./temp 生成alluredir 的json臨時報告到temp檔案下
testpaths = ./test_case #測驗case路徑
python_files = test*.py #模塊名規則
python_classes = Test* #類名規則
python_functions = test #方法名規則
生成報告
pytest-html(生成測驗報告)
allure-pytest(生成美觀的測驗報告)
if __name__ == '__main__':
pytest.main(['path/檔案名.py'])
os.system('allure generate ./temp -o ./report --clean')
框架介紹
資料驅動
關鍵字驅動
全域組態檔封裝
日志監控
斷言
報告郵件…
運行case腳本
import pytest
import os
if __name__ == '__main__':
pytest.main(['path/檔案名.py'])
# pytest.main(['-vs'])
os.system('allure generate ./temp -o ./report --clean')
資料驅動:
yaml格式
excel格式
csv格式
json格式
mysql
yaml格式資料驅動
yaml語法規則:
1.區分大小寫
2.和python一樣也是通過縮進的方式來表示層級關系(不同的是不能使用tab縮進,只能用空格縮進)
3.和縮進多少層無關,只和左邊是否對齊有關系
4.#表示注釋
yaml陣列組成舉例:
1.map物件 鍵:(空格)值
eg:
name: 小明
2.陣列(串列) :用一組橫線開頭
eg:
-name: 小明
-name: 小紅
讀取yaml檔案方法
import yaml
def read_yaml():
"""
讀取yaml檔案
:return:
"""
with open('檔案名',encoding='utf-8') as f:
data = yaml.load(f,loader=yaml.FullLoader)
return data
if __name__=='__main__':
print(read_yaml())
json資料驅動
json檔案讀取:
def RedJson(jsonurl):
# ss = os.path.dirname(os.path.abspath(__file__)) 拿到json路徑
f = os.path.join(jsonurl, 'test.json')
dict_data = {}
with open(f, 'r', encoding="utf-8") as fp:
dict_data = json.load(fp)
c_f = os.path.split(os.path.abspath(__file__))[1]
return list(dict_data.values())[0]
Request使用和封裝
Request使用
import requests
def test_baidu():
url = 'http://www.baid.com'
json={
"account": 1, "pageNum": 3, "pageSize": 4
}
r = requests.post(url=url,json=json) #url請求地址,json請求入參
print(r.text) #以text格式輸出回傳值
assert r.status_code == 200 #斷言
url的環境域名配置成全域引數
json存盤請求資料
from config.config import serverip #全域引數含url
from common.excel_value import get_excel_value #呼叫excel拿到請求引數
import requests #引入requests模塊
import os #引入os系統模塊
from common.getjson import RedJson #引入獲取json資料公共方法
import pytest #引入pytest模塊
jsonurl = os.path.dirname(os.path.abspath(__file__))
#將當前模塊目錄下的json檔案地址傳給獲取json資料的公共方法,拿到json資料
@pytest.fixture(scope="function")
def basic_data():
return RedJson(jsonurl)
#使用parametrize裝飾器入參
@pytest.mark.parametrize('account, pageNum, pageSize',
[
(1, 2, 3),
(1, 2, 3),
])
def test_queryList(account, pageNum, pageSize, basic_data):
url = serverip() + 'path地址/queryList'
json = {
"account": account, "designId": basic_data['designId'], "pageNum": pageNum, "pageSize": pageSize}
r = requests.post(url=url, json=json)
print(r.text)
assert r.status_code == 200
全域引數:
配置各環境域名,或者其他公共引數
def serverip():
'''
DEV= 開發環境的服務器ip
QA= 測驗環境的服務器ip
OL= 線上環境的服務器ip
:return: 回傳不同服務器的地址
'''
server_add={
'DEV' : 'http://his.xxxxxxxxxxx.com',
'QA' : 'http://www.baidu.qa/',
'OL' : 'http://www.baidu.ol/'
}
return server_add['QA']
常用框架介紹
common —存盤公共方法
config —存盤公共引數
report —存盤報告
temp —存盤臨時檔案
test_case —測驗case
test_case中,各個模塊下面存一個json檔案,用于存盤該模塊下用到的引數
test_data --存盤測驗資料(excel,CSV驅動資料)
pytest.ini —pytest的核心配置,上面有介紹
run.py —運行主方法

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/292316.html
標籤:其他
