一柱宦、Pytest優(yōu)點認知:
1.可以結(jié)合所有的自動化測試工具
2.跳過失敗用例以及失敗重跑
3.結(jié)合allure生產(chǎn)美觀報告
4.和Jenkins持續(xù)集成
5.很多強大的插件
pytest-html:生產(chǎn)html測試報告
pytest-xdist:多線程運行
pytest-ordering:改變用例執(zhí)行順序
pytest-rerunfailures:失敗用例重爬
allure-pytest:美觀測試報告
一般項目中构回,會使用requerments.text文檔保存插件名稱,進行批量一次性安裝
pip install -r requerments.txt
二、運行方式:
1.主函數(shù)運行方式:main方法運行
2.命令運行方式
pytest -vs
-v:更加詳細信息
-s:調(diào)試信息
-n=處理:多線程運行
--reruns=數(shù)字:失敗用例重跑
--reruns=數(shù)字:失敗用例重跑
--html=./report.html:生成html報告
用例分組運行
1.進行用例分組:
2.用例進行注解:
#@pytest.mark.分組名稱 如下:
@pytest.mark.smoke
[pytest]
##運行命令,例如: -vs -m "smoke"分組執(zhí)行名稱都是固定的
addopts = -vs
#測試用例文件目錄
testpaths = ./testcases
python_files = test_*.py
python_classes = Test*
python_functions = test_*
##分組
markers =
smoke:maoyan
case:gongneng
三、前置后置腐芍,夾具
1.簡單區(qū)分:直接調(diào)用方法,但是接口過多時试躏,比較麻煩
def setup(self):
print("每個用例執(zhí)行之前猪勇,都執(zhí)行一遍")
def teardown(self):
print("每個用例執(zhí)行之后,都執(zhí)行一遍")
2.實現(xiàn)部分前置:如只想之一個用例進行前置颠蕴,如登錄時需要連接數(shù)據(jù)庫泣刹。
需要使用裝置器:來實現(xiàn)
參數(shù)介紹:
@pytest.fixture(scope="作用域",params="數(shù)據(jù)驅(qū)動",autouse="是否自動執(zhí)行",ids="自定義參數(shù)",name="重命名")
作用域:可以函數(shù)助析、類、模塊椅您、包貌笨、session
使用方法:
1.需要前置的功能函數(shù)上進行標注裝置器
2.別的方法函數(shù)之間調(diào)用裝置器
如下:一個文件里面進行部分前置喚醒
import time
import pytest
import requests
#實現(xiàn)裝置器標注前置,進行標注襟沮,yieid進行喚醒返回
@pytest.fixture(scope="function")
def conn_getbase():
print("連接數(shù)據(jù)庫成功")
yield
print("關(guān)閉數(shù)據(jù)庫成功")
class TestSendRequsets:
#過多接口時,比較麻煩冗余
# def setup(self):
# print("每個用例執(zhí)行之前")
#
# def teardown(self):
# print("每個用例執(zhí)行之后")
def test_getImgCode(self):
# 接口url
t = time.time()
timess = str(int(round(t * 1000)))
times = str(int(t))
url = "http://124.71.230.185:9002/jeecg-boot/sys/randomImage/" + "" + timess
# 參數(shù)
data = {
"_t": times,
}
# # get請求
rep = requests.request('get', url, params=data)
print(rep.text)
# 標注為smoke分組用例
@pytest.mark.smoke
def test_Login(self,conn_getbase):
# post請求
url = "http://124.71.230.185:9002/jeecg-boot/sys/login"
# 參數(shù)
data = {
"captcha": "Gkak!@#2021",
"checkKey": 1637811815838,
"password": "123456",
"remember_me": 1,
"username": "admin"
}
rep = requests.request('post', url, json=data)
statues = rep.json()["success"]
message = rep.json()["message"]
if statues:
print("")
else:
# raise Exception(message)
print(message)
if __name__ == '__main__':
pytest.main();
3.封裝靈活調(diào)用
一般情況下:@pytest.fixture()會和conftest.py文件一塊使用
conftest.py名稱是固定的昌腰,功能如下:
1.用處是多個py文件之間共享前置配置开伏。
2.里面的方法在調(diào)用時,不需要導(dǎo)入遭商,可以之間調(diào)用
3.可以都多個conftest.py文件固灵,也可以有不同的層級
conftest.py文件詳情請看下一章
實現(xiàn):
1.目錄下之間創(chuàng)建conftest.py文件
2.把上面的這段代碼之間粘貼到conftest.py文件中
# 前置函數(shù)
import pytest
@pytest.fixture(scope="function")
def conn_getbase():
print("連接數(shù)據(jù)庫成功")
yield
print("關(guān)閉數(shù)據(jù)庫成功")