pytest hook 函數(shù)也叫鉤子函數(shù),pytest 提供了大量的鉤子函數(shù),可以在用例的不同生命周期自動(dòng)調(diào)用。 比如,在測(cè)試用例收集階段,可利用 hook 函數(shù)修改測(cè)試用例名稱的編碼。
pytest的hook是基于Python的插件系統(tǒng)實(shí)現(xiàn)的,使用特定的命名規(guī)范和裝飾器來(lái)定義鉤子函數(shù)。你可以在pytest插件或conftest文件中定義這些鉤子函數(shù)。
pytest 可以識(shí)別到三種插件
內(nèi)置插件:從 pytest 內(nèi)部 _pytest 目錄加載的插件
外部插件:通過(guò) pip 安裝的插件(比如: pip install pytest-ordering )。
conftest.py 插件:測(cè)試目錄中的 conftest.py 加載
常用的pytest鉤子函數(shù)
1. `pytest_configure(config)`: 在pytest啟動(dòng)時(shí)調(diào)用,可用于注冊(cè)自定義標(biāo)記、插件等。
2. `pytest_collection_modifyitems(config, items)`: 測(cè)試收集階段調(diào)用,可以修改、篩選或排序收集到的測(cè)試項(xiàng)。
3. `pytest_runtest_protocol(item, nextitem)`: 在運(yùn)行每個(gè)測(cè)試用例之前和之后調(diào)用,可以執(zhí)行初始化、清理或自定義測(cè)試流程等操作。
4. `pytest_report_teststatus(report)`: 在測(cè)試用例執(zhí)行后調(diào)用,可用于自定義測(cè)試結(jié)果報(bào)告。
5. `pytest_terminal_summary(terminalreporter)`: 在測(cè)試會(huì)話結(jié)束時(shí)調(diào)用,可用于生成自定義的測(cè)試總結(jié)報(bào)告。
通過(guò)使用這些鉤子函數(shù),你可以在pytest的不同階段添加自己的邏輯和行為。這使得您可以自定義測(cè)試收集、執(zhí)行、報(bào)告等方面,以滿足特定的需求。
有許多其他的pytest鉤子函數(shù)可以用于不同的目的和擴(kuò)展。你可以在pytest的官方文檔中查找完整的鉤子函數(shù)列表以及每個(gè)鉤子的詳細(xì)說(shuō)明和示例。
開(kāi)發(fā)插件
pytest 提供了大量的 hook 函數(shù),執(zhí)行過(guò)程中幾乎所有的行為都是可以定制的。那么,pytest 可以改寫(xiě)哪 些行為呢? 文字版 pytest hook 執(zhí)行順序:
root
└── pytest_cmdline_main
├── pytest_plugin_registered
├── pytest_configure
│ └── pytest_plugin_registered
├── pytest_sessionstart
│ ├── pytest_plugin_registered
│ └── pytest_report_header
├── pytest_collection
│ ├── pytest_collectstart
│ ├── pytest_make_collect_report
│ │ ├── pytest_collect_file
│ │ │ └── pytest_pycollect_makemodule
│ │ └── pytest_pycollect_makeitem
│ │ └── pytest_generate_tests
│ │ └── pytest_make_parametrize_id
│ ├── pytest_collectreport
│ ├── pytest_itemcollected
│ ├── pytest_collection_modifyitems
│ └── pytest_collection_finish
│ └── pytest_report_collectionfinish
├── pytest_runtestloop
│ └── pytest_runtest_protocol
│ ├── pytest_runtest_logstart
│ ├── pytest_runtest_setup
│ │ └── pytest_fixture_setup
│ ├── pytest_runtest_makereport
│ ├── pytest_runtest_logreport
│ │ └── pytest_report_teststatus
│ ├── pytest_runtest_call
│ │ └── pytest_pyfunc_call
│ ├── pytest_runtest_teardown
│ │ └── pytest_fixture_post_finalizer
│ └── pytest_runtest_logfinish
├── pytest_sessionfinish
│ └── pytest_terminal_summary
└── pytest_unconfigure
可以利用 pytest hook 強(qiáng)大的功能開(kāi)發(fā)出自己的插件。
hook和fixture的區(qū)別
在pytest中,Hooks(鉤子)和Fixtures(裝置)是兩個(gè)不同的概念。
Hooks(鉤子)是pytest提供的一組鉤子函數(shù),用于自定義和擴(kuò)展測(cè)試流程。鉤子函數(shù)在特定的時(shí)間點(diǎn)被調(diào)用,并允許你插入自定義的代碼來(lái)修改、補(bǔ)充或攔截測(cè)試操作,比如定制報(bào)告、自定義收集規(guī)則、執(zhí)行前/后的初始化和清理等。Hooks通常由插件或conftest文件定義,并使用特定的命名規(guī)范和裝飾器進(jìn)行標(biāo)記。使用hooks可以靈活地定制和擴(kuò)展pytest的行為。
Fixtures(裝置)是pytest的一項(xiàng)功能,用于管理測(cè)試用例的前置和后置操作。Fixture可以被看作為測(cè)試用例的準(zhǔn)備和清理工作,并且可以在多個(gè)測(cè)試用例之間共享數(shù)據(jù)和資源。Fixture函數(shù)使用`@pytest.fixture`裝飾器進(jìn)行標(biāo)記,并在測(cè)試函數(shù)的參數(shù)中使用。當(dāng)測(cè)試函數(shù)需要使用該裝置時(shí),fixture函數(shù)將被自動(dòng)執(zhí)行并提供必要的數(shù)據(jù)和資源。Fixture可以執(zhí)行一些初始化操作,為測(cè)試用例提供必要的數(shù)據(jù),以及在測(cè)試結(jié)束后進(jìn)行清理工作。使用fixture可以提高代碼的復(fù)用性和可維護(hù)性,并減少測(cè)試用例之間的重復(fù)工作。
總結(jié)起來(lái),Hooks允許你定制和擴(kuò)展測(cè)試流程,而Fixtures則用于管理測(cè)試用例的前置和后置操作,并提供必要的數(shù)據(jù)和資源。Hooks是用于自定義pytest的整體行為,而Fixtures是用于測(cè)試用例級(jí)別的準(zhǔn)備和清理工作。兩者可以一起使用,以實(shí)現(xiàn)更高級(jí)別的自定義和測(cè)試管理。
pytest_collection_modifyitems
def pytest_collection_modifyitems(
session: "Session", config: "Config", items: List["Item"]
) -> None:
"""Called after collection has been performed. May filter or re-order
the items in-place.
:param pytest.Session session: The pytest session object.
:param pytest.Config config: The pytest config object.
:param List[pytest.Item] items: List of item objects.
"""
可以看到該方法在用例收集后執(zhí)行,可以篩選或者對(duì)用例執(zhí)行順序進(jìn)行修改。?
插件開(kāi)發(fā)-篩選收集到的用例
當(dāng)你想要自定義收集階段中的測(cè)試項(xiàng)時(shí),可以使用`pytest_collection_modifyitems`鉤子函數(shù)。這個(gè)鉤子函數(shù)在pytest的測(cè)試收集過(guò)程中被調(diào)用,允許你對(duì)收集到的測(cè)試項(xiàng)進(jìn)行修改、篩選或排序。
下面是一個(gè)使用`pytest_collection_modifyitems`的例子,假設(shè)你希望在測(cè)試收集階段中只運(yùn)行有特定標(biāo)記的測(cè)試用例(比如`smoke`標(biāo)記),在根目錄的conftest.py中添加如下方法:
def pytest_collection_modifyitems(config, items):
? ? marked_items = []
? ? unmarked_items = []
? ? # 將有"smoke"標(biāo)記的測(cè)試項(xiàng)放入marked_items列表,其他放入unmarked_items列表
? ? for item in items:
? ? ? ? if 'smoke' in item.keywords:
? ? ? ? ? ? marked_items.append(item)
? ? ? ? else:
? ? ? ? ? ? unmarked_items.append(item)
? ? # 只保留有"smoke"標(biāo)記的測(cè)試項(xiàng)
? ? items[:] = marked_items
? ? # 打印被移除的未標(biāo)記項(xiàng)的名稱
? ? for item in unmarked_items:
? ? ? ? print(f"Skipping unmarked test: {item.nodeid}")
在上述代碼中,`pytest_collection_modifyitems`鉤子函數(shù)接收兩個(gè)參數(shù):`config`表示pytest的配置對(duì)象,`items`是測(cè)試收集階段收集到的所有測(cè)試項(xiàng)(測(cè)試用例)的列表。我們首先遍歷每個(gè)測(cè)試項(xiàng),將有"smoke"標(biāo)記的項(xiàng)放入`marked_items`列表,其他項(xiàng)放入`unmarked_items`列表。然后,我們通過(guò)將`items`列表替換為`marked_items`列表,實(shí)現(xiàn)只保留有"smoke"標(biāo)記的測(cè)試項(xiàng)。最后,我們遍歷`unmarked_items`列表,打印被移除的未標(biāo)記項(xiàng)的名稱作為提示信息。
可以看到test_b被跳過(guò)?
插件開(kāi)發(fā)-改寫(xiě)用例名稱編碼
測(cè)試用例如下
@pytest.mark.parametrize("name",["哈利","赫敏"])
def test_encode(name):
print(name)
運(yùn)行時(shí)會(huì)出現(xiàn)亂碼
在conftest.py中添加如下方法
def pytest_collection_modifyitems(config, items):
for item in items:
item._nodeid = item.nodeid.encode('utf-8').decode('unicode-escape')
再次運(yùn)行可以看到節(jié)點(diǎn)id顯示為中文了。
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-664093.html
?插件打包
軟件測(cè)試 | Pytest測(cè)試框架之插件開(kāi)發(fā)_pytest_hook_函數(shù)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-664093.html
到了這里,關(guān)于測(cè)試框架pytest教程(6)鉤子函數(shù)hook開(kāi)發(fā)pytest插件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!