柚子快報(bào)激活碼778899分享:用例管理框架pytest
柚子快報(bào)激活碼778899分享:用例管理框架pytest
一、作用:
1、發(fā)現(xiàn)用例:默認(rèn)發(fā)現(xiàn)用例的規(guī)則。模塊名必須以test_開頭或者_(dá)test結(jié)尾,測試類必須以Test開頭,測試方法必須以test_開頭
2、執(zhí)行用例
3、判斷結(jié)果
4、生成結(jié)果
二、Pytest用例管理框架詳細(xì)介紹
結(jié)合sele,reque,appium實(shí)現(xiàn)web,接口,app自動(dòng)化。
Allure生成非常美觀的報(bào)告以及和jenkins實(shí)現(xiàn)持續(xù)集成
插件:pytest;
pytest-html;生成html報(bào)告
pytest-xdist? 多線程執(zhí)行
pytest-ordering? 控制用例的執(zhí)行順序
pytest-rerunfailures? 失敗用例重跑
pytest-base-url? 基礎(chǔ)路徑
allure-pytest? 生成allure報(bào)告
---------上述均放到requirements.txt里面,通過以下命令安裝
pip install -r requirements.txt
三、如何執(zhí)行
1、命令行?不行現(xiàn)在
2、主函數(shù)
import pytest
if __name__ == '__main__':
pytest.main()
3、通過配置文件pytest.ini來改變以及執(zhí)行用例
不管是命令行還是主函數(shù),都會讀取pytest.ini配置文件來執(zhí)行。
[pytest]
#配置參數(shù)
addopts = -vs -m "smoke"
#改變用例的查找規(guī)則
testpaths = ./testcases
#改變模塊的查找規(guī)則
python_files = test_*.py
#改變函數(shù)的查找規(guī)則
python_functions = test_*
#標(biāo)記
markers =
smoke:冒煙測試
先添加標(biāo)簽【python】
addopts = -vs : 輸出調(diào)試&詳細(xì)信息
-s:輸出調(diào)試信息,包括print打印的信息 -v:顯示更詳細(xì)的信息 -q:顯示簡略信息,與-v作用相反 -p no:warning :過濾警告 -p no:randomly :disable隨機(jī)執(zhí)行 -n=num:啟用多線程或分布式運(yùn)行測試用例。需要安裝pytest-xdist插件模塊 -k=value:py文件中用例包含value的用例都會被執(zhí)行 -m=標(biāo)簽:執(zhí)行被@pytest.mark.標(biāo)簽名標(biāo)記的用例 -x:只要有一個(gè)用例執(zhí)行失敗就停止當(dāng)前線程的測試執(zhí)行 –maxfail=num:與-x功能一樣,可以自定義用例失敗次數(shù) –rerun=num:失敗用例重跑,需要安裝pytest-rerunfailures插件模塊 -l:展示運(yùn)行過程中的全局變量和局部變量 –collect-only:羅列出所有當(dāng)前目錄下所有的測試模塊,測試類及測試函數(shù) –ff:如果上次測試用例出現(xiàn)失敗的用例,當(dāng)使用–ff后,失敗的測試用例會首先執(zhí)行,剩余的用例也會再次執(zhí)行 –lf:當(dāng)一個(gè)或多個(gè)用例失敗后,定位到最后一個(gè)失敗的用例重新運(yùn)行,后續(xù)用例會停止運(yùn)行 –html=report.html:當(dāng)前目錄生成名為report.html的測試報(bào)告,需要安裝pytest.html插件模塊 ———————————————— 版權(quán)聲明:上述參數(shù)總結(jié)詳見CSDN博主「鹿上的程序媛」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。 原文鏈接:https://blog.csdn.net/qq_41571224/article/details/124517351
四、Pytest用例管理框架的前后置(固件,夾具)
1、作用:在你的用例或類前后執(zhí)行相應(yīng)的操作
class TestMAP(object):
now_time = ""
def setup(self):
print("每個(gè)用例前的操作")
def teardown(self):
print("每個(gè)用例后的操作")
def setup_class(self):
print("每個(gè)類后的操作")
def teardown_class(self):
print("每個(gè)類后的操作")
2、前后置工具:fixture固件,夾具
裝飾器:
@pytest.fixture(scope = "作用域",params="參數(shù)化",autouse="自動(dòng)執(zhí)行",ids=“參數(shù)別名”,name = "")
scope:function, class, module, session
params: 參數(shù)化["qqqq","ewee"]
ids:參數(shù)別名["q","e"]
name:夾具別名
a、
@pytest.fixture(scope="function", autouse=True)
def description():
print("\n用例開始執(zhí)行")
yield
print("\n用例執(zhí)行結(jié)束")
@pytest.fixture(scope="function", autouse=False)
def description():
print("\n用例開始執(zhí)行")
yield
print("\n用例執(zhí)行結(jié)束")
# 執(zhí)行上述方式:def test_navi_main(self,description):
b、
@pytest.fixture(scope="function", autouse=False, params=["feature1", "feature2"])
def description(request):
print("\n用例開始執(zhí)行")
yield request.param
print("\n用例執(zhí)行結(jié)束")
# 執(zhí)行上述方式:def test_navi_main(self,description):
# def test_print_date(self, description):
# print(description)
配置參數(shù):
base_url:https://api.weixin.qq.com
函數(shù)中適用時(shí):
def test_navi_main(self,base_url):
? ? ? ? url = base_url+"XXXXXX"
allure報(bào)告:如需要allure報(bào)告顯示,導(dǎo)入allure模塊,并將每條用例加allure標(biāo)簽。
詳細(xì)說明找時(shí)間仔細(xì)梳理一下,此處不更新
柚子快報(bào)激活碼778899分享:用例管理框架pytest
推薦閱讀
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。