Pytest_Hook鈎子函數總結(14)


前言

pytest 的鈎子函數有很多,通過鈎子函數的學習可以了解到pytest在執行用例的每個階段做什么事情,也方便后續對pytest二次開發學習。
詳細文檔可以查看pytest官方文檔https://docs.pytest.org/en/latest/reference/reference.html?highlight=hook#hooks

鈎子函數總結

第一部分:setuptools

引導掛鈎要求足夠早注冊的插件(內部和setuptools插件),可以使用的鈎子

  • pytest_load_initial_conftests(early_config,parser,args): 在命令行選項解析之前實現初始conftest文件的加載。
  • pytest_cmdline_preparse(config,args): (不建議使用)在選項解析之前修改命令行參數。
  • pytest_cmdline_parse(pluginmanager,args): 返回一個初始化的配置對象,解析指定的args。
  • pytest_cmdline_main(config): 要求執行主命令行動作。默認實現將調用configure hooks和runtest_mainloop。

第二部分: 初始化掛鈎

初始化鈎子需要插件和conftest.py文件

  • pytest_addoption(parser): 自定義命令行參數。注冊argparse樣式的選項和ini樣式的配置值,這些值在測試運行開始時被調用一次。查看用法
  • pytest_addhooks(pluginmanager): 在插件注冊時調用,以允許通過調用來添加新的掛鈎
  • pytest_configure(config): 允許插件和conftest文件執行初始配置。
  • pytest_unconfigure(config): 在退出測試過程之前調用。
  • pytest_sessionstart(session): 在Session創建對象之后,執行收集並進入運行測試循環之前調用。
  • pytest_sessionfinish(session,exitstatus): 在整個測試運行完成后調用,就在將退出狀態返回系統之前。
  • pytest_plugin_registered(plugin,manager):一個新的pytest插件已注冊。

第三部分: collection 收集鈎子

  • pytest_collection(session): 執行給定會話的收集協議。
  • pytest_collect_directory(path, parent): 在遍歷目錄以獲取集合文件之前調用。
  • pytest_collect_file(path, parent) 為給定的路徑創建一個收集器,如果不相關,則創建“無”。
  • pytest_pycollect_makemodule(path: py._path.local.LocalPath, parent) 返回給定路徑的模塊收集器或無。
  • pytest_pycollect_makeitem(collector: PyCollector, name: str, obj: object) 返回模塊中Python對象的自定義項目/收集器,或者返回None。在第一個非無結果處停止
  • pytest_generate_tests(metafunc: Metafunc) 生成(多個)對測試函數的參數化調用。
  • pytest_make_parametrize_id(config: Config, val: object, argname: str) 返回val 將由@ pytest.mark.parametrize調用使用的給定用戶友好的字符串表示形式,如果掛鈎不知道,則返回None val。
  • pytest_collection_modifyitems(session: Session, config: Config, items: List[Item]) 在執行收集后調用。可能會就地過濾或重新排序項目。
  • pytest_collection_finish(session: Session) 在執行並修改收集后調用。

第四部分:測試運行(runtest)鈎子

  • pytest_runtestloop(session: Session) 執行主運行測試循環(收集完成后)。
  • pytest_runtest_protocol(item: Item, nextitem: Optional[Item]) 對單個測試項目執行運行測試協議。
  • pytest_runtest_logstart(nodeid: str, location: Tuple[str, Optional[int], str]) 在運行單個項目的運行測試協議開始時調用。
  • pytest_runtest_logfinish(nodeid: str, location: Tuple[str, Optional[int], str])在為單個項目運行測試協議結束時調用。
  • pytest_runtest_setup(item: Item) 調用以執行測試項目的設置階段。
  • pytest_runtest_call(item: Item) 調用以運行測試項目的測試(調用階段)。
  • pytest_runtest_teardown(item: Item, nextitem: Optional[Item]) 調用以執行測試項目的拆卸階段。
  • pytest_runtest_makereport(item: Item, call: CallInfo[None]) 被稱為為_pytest.reports.TestReport測試項目的每個設置,調用和拆卸運行測試階段創建一個。
  • pytest_pyfunc_call(pyfuncitem: Function) 調用基礎測試功能。

第五部分:Reporting 報告鈎子

  • pytest_collectstart(collector: Collector) 收集器開始收集。
  • pytest_make_collect_report(collector: Collector) 執行collector.collect()並返回一個CollectReport。
  • pytest_itemcollected(item: Item) 我們剛剛收集了一個測試項目。
  • pytest_collectreport(report: CollectReport) 收集器完成收集。
  • pytest_deselected(items: Sequence[Item]) 要求取消選擇的測試項目,例如按關鍵字。
  • pytest_report_header(config: Config, startdir: py._path.local.LocalPath) 返回要顯示為標題信息的字符串或字符串列表,以進行終端報告。
  • pytest_report_collectionfinish(config: Config, startdir: py._path.local.LocalPath, items: Sequence[Item]) 返回成功完成收集后將顯示的字符串或字符串列表。
  • pytest_report_teststatus(report: Union[CollectReport, TestReport], config: Config) 返回結果類別,簡寫形式和詳細詞以進行狀態報告。
  • pytest_terminal_summary(terminalreporter: TerminalReporter, exitstatus: ExitCode, config: Config) 在終端摘要報告中添加一個部分。
  • pytest_fixture_setup(fixturedef: FixtureDef[Any], request: SubRequest) 執行夾具設置執行。
  • pytest_fixture_post_finalizer(fixturedef: FixtureDef[Any], request: SubRequest) 在夾具拆除之后但在清除緩存之前調用,因此夾具結果fixturedef.cached_result仍然可用(不是 None)
  • pytest_warning_captured(warning_message: warnings.WarningMessage, when: Literal[‘config’, ‘collect’, ‘runtest’], item: Optional[Item], location: Optional[Tuple[str, int, str]]) (已棄用)處理內部pytest警告插件捕獲的警告。
  • pytest_warning_recorded(warning_message: warnings.WarningMessage, when: Literal[‘config’, ‘collect’, ‘runtest’], nodeid: str, location: Optional[Tuple[str, int, str]]) 處理內部pytest警告插件捕獲的警告。
  • pytest_runtest_logreport(report: TestReport) 處理項目的_pytest.reports.TestReport每個設置,調用和拆卸運行測試階段產生的結果。
  • pytest_assertrepr_compare(config: Config, op: str, left: object, right: object) 返回失敗斷言表達式中的比較的說明。
  • pytest_assertion_pass(item: Item, lineno: int, orig: str, expl: str) (實驗性的)在斷言通過時調用。

第六部分:調試/相互作用鈎

很少有可以用於特殊報告或與異常交互的掛鈎:

  • pytest_internalerror(excrepr: ExceptionRepr, excinfo: ExceptionInfo[BaseException]) 要求內部錯誤。返回True以禁止對將INTERNALERROR消息直接打印到sys.stderr的回退處理。
  • pytest_keyboard_interrupt(excinfo: ExceptionInfo[Union[KeyboardInterrupt, Exit]]) 要求鍵盤中斷。
  • pytest_exception_interact(node: Union[Item, Collector], call: CallInfo[Any], report: Union[CollectReport, TestReport]) 在引發可能可以交互處理的異常時調用。
  • pytest_enter_pdb(config: Config, pdb: pdb.Pdb) 調用了pdb.set_trace()。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM