柚子快報激活碼778899分享:pytest之鉤子函數(shù)
在pytest中鉤子函數(shù)(hook functions)是用來自定義和擴展 pytest 的功能的關鍵機制。這些鉤子函數(shù)可以在 pytest 的配置文件中或者插件中實現(xiàn),用于干預測試執(zhí)行的不同階段和行為,這篇文章主要用來記錄他不同鉤子函數(shù)的使用
我們在開發(fā)自動化測試平臺的時候其實可以借助于鉤子函數(shù)來調用pytest測試腳本
首先簡單定義的我們的測試函數(shù)如下test_demo.py:
def test_one():
a = 100
assert a < 100
def test_two():
a = 100
assert a == 100
def test_three():
a = 200
assert a > 100
上面就是我們的測試函數(shù)了,那我們一切以測試函數(shù)為準
我們鉤子函數(shù),我們都在pytest這個項目目錄的根目錄下創(chuàng)建我們的conftest.py文件,這樣執(zhí)行pytest測試的時候會自動觸發(fā)我們這個文件中鉤子函數(shù)
鉤子函數(shù)之:pytest_collection_modifyitems(session, config, items)
1.用于修改或者重新排序收集到的測試項(test items),在items這個參數(shù)中
2.可以通過這個鉤子來動態(tài)地過濾測試用例或者改變它們的執(zhí)行順序
下面是我們定義的函數(shù):
def pytest_collection_modifyitems(session, config, items):
"""
:param session:當前 pytest 會話對象
:param config:當前的配置對象
:param items:包含所有已收集測試項的列表
:return:
"""
print("Modifying collected items...")
print(items)
for i in items:
print(i.name)
當我們使用?pytest .\test_demo.py 命令行啟動我們測試的時候,我們可以看到如下打?。?
========================================================================= ?test session starts ========================================================================== platform win32 -- Python 3.10.5, pytest-8.3.1, pluggy-1.5.0 rootdir: C:\Users\w\Plants\pytest_demo collecting ... Modifying collected items... [
test_demo.py F.. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[100%]
=============================================================================== FAILURES ===============================================================================? _______________________________________________________________________________ test_one _______________________________________________________________________________?
? ? def test_one(): ? ? ? ? a = 100 > ? ? ? assert a < 100 E ? ? ? assert 100 < 100
test_demo.py:3: AssertionError ======================================================================= short test summary info ========================================================================? FAILED test_demo.py::test_one - assert 100 < 100 ===================================================================== 1 failed, 2 passed in 0.04s ======================================================================?
我們可以直接看到在test session starts 之后這里直接觸發(fā)了我們的Modifying collected items...,證明在測試執(zhí)行直接,會直接觸發(fā)這個鉤子函數(shù),我們打印的參數(shù)
items:[
我們用for循環(huán)去一個個循環(huán)打印,會發(fā)現(xiàn),這些函數(shù)其實是能點出來函數(shù)名的,
test_one,test_two,test_three按照收集的順序打印出來,他執(zhí)行也是這個列表的順序
那么我們就可以在這個里面進行一些操作
改變收集到的測試case,去掉某些case執(zhí)行:
def pytest_collection_modifyitems(session, config, items):
"""
:param session:當前 pytest 會話對象
:param config:當前的配置對象
:param items:包含所有已收集測試項的列表
:return:
"""
print("Modifying collected items...")
print(items)
new_items = []
for i in items:
if i.name != 'test_one':
new_items.append(i)
print('select case list is {}'.format(new_items))
items[:] = new_items
在這個代碼中,我們將收集的case內容發(fā)生了改變,這樣就會執(zhí)行我們想要執(zhí)行的特定case
?C:\Users\w\Plants\pytest_demo> pytest .\test_demo.py ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.10.5, pytest-8.3.1, pluggy-1.5.0 rootdir: C:\Users\w\Plants\pytest_demo collecting ... Modifying collected items... [
test_demo.py .. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [100%]?
========================================================================== 2 passed in 0.01s ===========================================================================?
在上面命令輸出中,我們可以明顯的看到,自動收集到的是3個case,但是因為我們中間做出了改變,最后實際執(zhí)行的只有2個case,這兩個才是我們真正想要執(zhí)行的
在我們實際項目中,會存在case的自由組合執(zhí)行,那么這個時候可能平臺下發(fā)的case列表就可以做這樣的操作,如下,我的case列表是最終寫入在了一個json文件中,那么可以做這樣的更改
test_case.json
{
"test_case_list": [
"test_one",
"test_three"
]
}
那么在我的鉤子函數(shù)中,我進行這樣的修改:
def pytest_collection_modifyitems(session, config, items):
"""
:param session:當前 pytest 會話對象
:param config:當前的配置對象
:param items:包含所有已收集測試項的列表
:return:
"""
print("Modifying collected items...")
with open(r'test_case.json', 'r', encoding='utf8') as f:
test_case_dict = json.load(f)
test_case_list = test_case_dict['test_case_list']
new_items = [item for item in items if item.name in test_case_list]
print('select case list is {}'.format(new_items))
items[:] = new_items
現(xiàn)在執(zhí)行的打印就變成了
pytest .\test_demo.py -v? ========================================================================= test session starts ========================================================================== platform win32 -- Python 3.10.5, pytest-8.3.1, pluggy-1.5.0 -- D:\Virtualenvs\Plants\Scripts\python.exe cachedir: .pytest_cache rootdir: C:\Users\w\Plants\pytest_demo collecting ... Modifying collected items... select case list is [
test_demo.py::test_one FAILED ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [ 50%] test_demo.py::test_three PASSED ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [100%]?
=============================================================================== FAILURES ===============================================================================? _______________________________________________________________________________ test_one _______________________________________________________________________________?
? ? def test_one(): ? ? ? ? a = 100 > ? ? ? assert a < 100 E ? ? ? assert 100 < 100
test_demo.py:3: AssertionError ======================================================================= short test summary info ========================================================================? FAILED test_demo.py::test_one - assert 100 < 100 ===================================================================== 1 failed, 1 passed in 0.03s ======================================================================?
我們可以看到收集的還是3個case,但是我們直接用json文件中的test_case_list的case,這樣最終執(zhí)行的其實是我們想要執(zhí)行的test_one跟test_three,這樣就可以動態(tài)去改變執(zhí)行case,實現(xiàn)從平臺下發(fā)的case自由組合
其他鉤子函數(shù)
暫時還沒有實際用到,記錄一下:
pytest_configure(config):
當 pytest 開始執(zhí)行時調用。允許插件或者配置文件在整個測試運行之前執(zhí)行一些初始化操作。 pytest_unconfigure(config):
當 pytest 執(zhí)行完成后調用??梢杂糜趫?zhí)行一些清理操作或者收尾工作。 pytest_addoption(parser, pluginmanager):
用于添加額外的命令行選項。允許插件向 pytest 添加自定義的命令行選項,以控制測試運行的行為。 pytest_generate_tests(metafunc):
在測試收集階段動態(tài)生成測試參數(shù)??梢愿鶕枰獎討B(tài)生成測試參數(shù),例如從外部數(shù)據源加載測試數(shù)據并生成測試用例。 pytest_runtest_setup(item):
在每個測試用例執(zhí)行 setup 階段之前調用。允許執(zhí)行一些預備操作或者設置步驟,例如創(chuàng)建臨時資源或者初始化測試環(huán)境。 pytest_runtest_call(item):
在每個測試用例執(zhí)行測試函數(shù)時調用??梢栽谶@里實現(xiàn)對測試用例執(zhí)行過程的監(jiān)控或者記錄。 pytest_runtest_teardown(item):
在每個測試用例執(zhí)行 teardown 階段之后調用??梢杂糜谇謇頊y試用例執(zhí)行過程中創(chuàng)建的資源或者狀態(tài)恢復。 pytest_sessionfinish(session, exitstatus):
在整個測試會話結束時調用。允許進行最終的報告生成、日志記錄或者資源釋放等操作。
?有個有意思的鉤子函數(shù):
def pytest_sessionfinish(session, exitstatus):
"""
:param session: 當前測試會話對象
:param exitstatus: 測試退出碼
:return:
"""
print(exitstatus)
# 如果我們全部都執(zhí)行成功了這exitstatus就是0
# 如果有FAILED 這個exitstatus 就是ExitCode.TESTS_FAILED 代表有執(zhí)行失敗的case
可以按照這個退出碼來判斷我們case是不是執(zhí)行成功了,如果這個exitstatus不為0,那么就代表這次測試肯定是有失敗的,那么我們之間按照這個給平臺發(fā)送一個失敗的通知,這樣不會從日志中去判斷獲取了
柚子快報激活碼778899分享:pytest之鉤子函數(shù)
推薦鏈接
本文內容根據網絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉載請注明,如有侵權,聯(lián)系刪除。