python接口自動(dòng)化-pytest-插件種類

# -*- coding: utf-8 -*-
# @Time    : 2023/6/26 20:39
# @Author  : yanfa
# @user   : yanfa 
# @File    : test_pytest_for_plugins.py
# @remark: pytest插件-內(nèi)置插件hook
""""""

"""一、pytest插件分類
1、外部插件:pip install安裝的插件,也叫第三方庫
2、本地插件:pytest自動(dòng)模塊發(fā)現(xiàn)機(jī)制(conftest.py存放的)
3、內(nèi)置插件:代碼內(nèi)部的_pytest目錄加載,也叫hook函數(shù)
"""

"""二、pytest hook介紹
1、是一個(gè)函數(shù),在系統(tǒng)消息觸發(fā)時(shí)被系統(tǒng)調(diào)用
2、自動(dòng)觸發(fā)機(jī)制
3、hook函數(shù)的名稱是確定的
4、pytest有很多的勾子函數(shù)
5、使用時(shí)直接編寫函數(shù)體

"""

"""三、pytest hook 執(zhí)行順序 
【參考https://ceshiren.com/t/topic/8807】
1、用例執(zhí)行順序:
大概 開始執(zhí)行->執(zhí)行測(cè)試用例->結(jié)束執(zhí)行
詳細(xì) 收集測(cè)試用例->收集完測(cè)試用例之后->執(zhí)行前置setup->調(diào)用執(zhí)行測(cè)試->執(zhí)行后置teardown->...->獲取測(cè)試結(jié)果
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

2、hook執(zhí)行順序 
見源碼 /site-packages/_pytest/hookspec.py

pytest_addoption: 添加命令行參數(shù),運(yùn)行時(shí)會(huì)先去讀取命令行參數(shù)
pytest_collection_modifyitems: 收集測(cè)試用例,收集后(改編碼,改執(zhí)行順序等)
pytest_collection_finish: 收集之后的操作
pytest_runtest_setup: 前置操作,在調(diào)用pytest_runtest_call之前調(diào)用
pytest_runtest_call: 調(diào)用執(zhí)行測(cè)試的用例
pytest_runtest_teardown: 后置操作,在調(diào)用pytest_runtest_call之后調(diào)用
pytest_runtest_makereport: 運(yùn)行測(cè)試用例,返回setup、call、teardown的執(zhí)行結(jié)果

改造腳本見conftest.py,復(fù)制hookspec.py中勾子函數(shù),然后函數(shù)體寫自己的邏輯
from _pytest.hookspec import hookspec

def pytest_runtest_setup(item: "Item") -> None:
    print("hook: 前置處理")

def pytest_runtest_call(item: "Item") -> None:
    print("hook: 執(zhí)行用例")

def pytest_runtest_teardown(item: "Item", nextitem: Optional["Item"]) -> None:
    print("hook: 后置處理")
"""
# 例子:conftest.py簡(jiǎn)單改造,然后執(zhí)行test_hook.py
# 輸出
# hook: 前置處理
# hook: 執(zhí)行用例
# 這是用例的操作 PASSED
# hook: 后置處理
def test_hook():
    print("這是用例的操作")


"""四、總結(jié)
1、hook函數(shù)名稱固定
2、hook函數(shù)會(huì)被自動(dòng)執(zhí)行
3、執(zhí)行是有先后順序的
4、pytest定義了很多的hook函數(shù),可以在不同階段實(shí)現(xiàn)不同的功能
"""

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容