網(wǎng)站建設(shè)運(yùn)營協(xié)議書2023年6月份又封城了
這里寫目錄標(biāo)題
- 一、pytest用法總結(jié)
- 二、pytest.ini是什么
- 三、改變運(yùn)行規(guī)則
- pytest.ini
- check_demo.py
- 執(zhí)行測試用例
- 四、添加默認(rèn)參數(shù)
- 五、指定執(zhí)行目錄
- 六、日志配置
- 七、pytest插件分類
- 八、pytest常用插件
- 九、改變測試用例的執(zhí)行順序
- 十、pytest并行與分布式執(zhí)行
- 十一、pytest內(nèi)置插件hook體系
- 十二、pytest插件開發(fā)
- 1、pytest_collection_modifyitems
- 2、pytest編寫插件——添加命令行參數(shù)(***)
- conftest.py
- test_option.py
一、pytest用法總結(jié)
1、修改用例的命名規(guī)則
2、配置日志格式、比代碼配置更方便
3、指定執(zhí)行目錄
4、排除搜索目錄
5、添加標(biāo)簽,防止運(yùn)行過程報警告
6、添加默認(rèn)參數(shù)
二、pytest.ini是什么
pytest.ini是pytest的配置文件
可以修改pytest的默認(rèn)行為
不能使用任何中文字符,包括漢字、空格、中文引號、中文冒號、中文注釋
三、改變運(yùn)行規(guī)則
執(zhí)行check_開頭和 test_開頭的所有的文件,后面一定要加*
python_files = check * test *
執(zhí)行所有的以Test和Check開頭的類
python_classes = Test* Check*
執(zhí)行所有以test_和check_開頭的方法
python_functions= test_* check_*
pytest.ini
pytest.ini中不能加注釋
check_demo.py
import pytest
import loggingclass CheckDemo:def check_demo1(self):logging.info('這是demo1測試用例')assert 1==1def check_demo2(self):logging.info('這是demo1測試用例')assert 1==1def test_demo1(self):logging.info('這是demo1測試用例')assert 1==2
執(zhí)行測試用例
pytest check_demo.py
四、添加默認(rèn)參數(shù)
addopts = -v -s
五、指定執(zhí)行目錄
testpaths= demo1
忽略某些目錄
norecursedirs = demo1 test_demo
六、日志配置
七、pytest插件分類
外部插件:pip install 插件
本地插件:pytest自動發(fā)現(xiàn)機(jī)制(conftest/py存放)
內(nèi)置插件:代碼內(nèi)部的_pytest目錄加載
八、pytest常用插件
pip install pytest-ordering:控制用例執(zhí)行順序
pip install pytest-xdist:分布式并發(fā)執(zhí)行測試用例
pip install pytest-dependency:控制用例的依賴關(guān)系
pip install pytest-rerunfailures:用例失敗重跑
pip install pytest-assume:多重校驗
pip install pytest-random-order:用例隨機(jī)執(zhí)行
pip install pytest-html:測試報告
九、改變測試用例的執(zhí)行順序
安裝;pip install pytest-ordering
使用:裝飾器:@pytest.mark.run(order=num)
,安裝數(shù)字從小到大的順序執(zhí)行。
pytest默認(rèn)從上到下執(zhí)行測試用例
import pytestclass TestB:@pytest.mark.run(order=2)def test_c(self):pass@pytest.mark.run(order=1)def test_d(self):pass
執(zhí)行測試用例
十、pytest并行與分布式執(zhí)行
安裝:pip install xdist
注意:用例多的時候效果明顯,多進(jìn)程并發(fā)執(zhí)行,同時支持allure
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2023/2/16 21:15
# @Author : 杜蘭特
# @File : test_xdist.py
import timeimport pytestclass TestC:def test_e(self):time.sleep(1)assert Truedef test_f(self):time.sleep(1)assert Truedef test_g(self):time.sleep(1)assert Truedef test_e1(self):time.sleep(1)assert Truedef test_f2(self):time.sleep(1)assert Truedef test_g3(self):time.sleep(1)assert True
執(zhí)行測試用例
-n auto:電腦默認(rèn)cpu核數(shù)
D:\pytest_project>pytest -n auto
十一、pytest內(nèi)置插件hook體系
1、hook函數(shù)名字固定
2、hook函數(shù)會被自動執(zhí)行
3、執(zhí)行是有先后順序的
4、pytest定義了很多hook函數(shù),可以在不同階段實現(xiàn)不同的功能
5、pytest有很多鉤子函數(shù)
6、使用時直接編寫函數(shù)體
十二、pytest插件開發(fā)
pytest_collection_modifyitems收集上來的測試用例實現(xiàn)定制化功能
解決問題:
自定義用例的執(zhí)行順序
解決編碼問題(中文的測試用例名稱)
自動添加標(biāo)簽
1、pytest_collection_modifyitems
# 收集完測試用例 之后調(diào)用的hook函數(shù)
def pytest_collection_modifyitems(items):"""測試用例收集完成時,將收集到的用例名name和用例標(biāo)識nodeid的中文信息顯示在控制臺上"""print(items)#name:用例的名字#nodeid:測試用例的路徑for item in items:item.name=item.name.encode('utf-8').decode('unicode-escape')item._nodeid=item.nodeid.encode('utf-8').decode('unicode-escape')items.reverse()
2、pytest編寫插件——添加命令行參數(shù)(***)
conftest.py
#定義一個命令行參數(shù)
def pytest_addoption(parser):mygroup = parser.getgroup("work") #group將下面所有的 option都展示在這個group下。mygroup.addoption("--env", #注冊一個命令行選項default = 'test', # 參數(shù)的默認(rèn)值dest = 'env', # 存儲的變量 為屬性命令,可以使用option對象訪問到這個值,暫用不到help = 'set your run env' # 幫助提示 參數(shù)的描述信息)#如何針對傳入的不同參數(shù)完成不同的邏輯處理
@pytest.fixture(scope='session')
def cmdoption(request):myenv=request.config.getoption('--env',default='test')if myenv == 'test':datapath='datas/test.yaml'elif myenv == 'dev':datapath='datas/env.yaml'with open(datapath) as f:datas=yaml.safe_load(f)return myenv,datas
test_option.py
def test_addoption(cmdoption):print(cmdoption)
如果命令行不傳–env參數(shù),env環(huán)境默認(rèn)為test
env環(huán)境需要dev的環(huán)境數(shù)據(jù),命令行傳入–env dev
D:\pytest_project\demo_plugin1>pytest test_option.py --env dev