文 | Blazer
來源|微信公眾號 Qtest之道
pytest是一個基於python、功能強大的測試框架,很多項目會採用該框架維護測試用例。pytest除了強大的用例發現、收集、執行和報告輸出之外,還提供了豐富的函數hook可以自定義插件或者框架行為,只需在項目目錄/conftest.py文件中實現對應hook名稱的函數,即可完成hook函數的註冊。
下面來為大家介紹一下pytest中常用hook都有哪些
引導型hook引導型hook僅在內部或setuptools安裝的插件中才會調用pytest_load_initial_conftests(early_config, parser, args)
實現了在命令行參數解析之前調用conftest文件pytest_cmdline_parse(pluginmanager, args)
解析指定參數並返回初始化的配置對象,返回第一個非None結果後停止調用後續實現pytest_cmdline_main(config)
用於執行命令行指令。默認實現中會調用配置hook和測試運行的主循環
初始化型hook初始化型hook主要在插件和conftest.py文件中調用pytest_addoption(parser)
註冊argparse風格的選項和ini配置風格的配置值,在測試開始時調用一次。註冊的值在config獲取到,而config可以通過很多內部對象的.config屬性獲取,或者使用pytestconfig夾具來獲取pytest_addhooks(pluginmanager)
在插件註冊時調用以添加新的hook,其可被其他插件實現用以改變本插件或者同本插件交互pytest_configure(config)
在命令行參數解析完成之後,以及插件和conftest文件加載之前調用。用於插件和conftest文件執行初始化操作。pytest_unconfigure(config)
測試進程退出之前調用pytest_sessionstart(session)
在Session對象創建之後,執行運行收集之前調用pytest_sessionfinish(session, exitstatus)
在測試運行結束之後,返回退出碼之前調用
測試運行hookpytest_runtestloop(session)
調用以開始運行測試用例循環pytest_runtest_protocol(item, nextitem)
用於實現runtest_setup/call/teardown協議,包括收集異常和調用報告hookpytest_runtest_logstart(nodeid, location)
在用例setup之前調用pytest_runtest_logfinish(nodeid, location)
在用例teardown之後調用pytest_runtest_setup(item)
在用例執行之前調用pytest_runtest_call(item)
調用以執行用例pytest_runtest_teardown(item, nextitem)
在用例執行完成後調用pytest_runtest_makereport(item, call)
在runtest_setup/call/teardown之後執行,用於返回當前運行的結果
用例收集hookpytest_collection(session)
在用例收集之前調用,用於在指定的session上收集用例pytest_ignore_collect(path, config)
返回True則不會收集當前路徑下的用例,所有文件/目錄開始收集前均會調用該hook查詢是否繼續pytest_collect_directory(path, parent)
在遍歷目錄中文件之前調用pytest_collect_file(path, parent)
返回Node或者None作為收集的用例pytest_pycollect_makeitem(collector, name, obj)
為模塊中的python對象返回自定義用例pytest_generate_tests(metafunc)
生成參數化的用例pytest_make_parametrize_id(config, val, argname)
生成自定義的參數化用例idpytest_collection_modifyitems(session, config, items)
用例收集完成之後,可用來添加、刪除或者排序收集好的用例
報告型hookpytest_collectstart(collector)
用例開始收集之前pytest_itemcollected(item)
收集一個用例之後pytest_collectreport(report)
收集完成之後pytest_deselected(items)
通過關鍵字刪除用例pytest_report_header(config, startdir)
定義報告標題pytest_report_collectionfinish(config, startdir, items)
定義顯示在"collected X items"之後的文字pytest_report_teststatus(report)
設置用例測試結果、錯誤信息和狀態信息pytest_terminal_summary(terminalreporter, exitstatus)
用於設置測試總結信息(運行完成後最後一行的 "X failed, Y passed")pytest_fixture_setup(fixturedef, request)
執行夾具的setup操作,返回結果會作為對應夾具的輸入pytest_fixture_post_finalizer(fixturedef, request)
在夾具teardown,cache清空前執行pytest_runtest_logreport(report)
在測試setup/call/teardown之後執行,可以用於自定義相關信息pytest_assertrepr_compare(config, op, left, right)
定義出錯斷言的解釋信息
debug/交互型 hookpytest_internalerror(excrepr, excinfo)
發生內部錯誤時調用pytest_keyboard_interrupt(excinfo)
鍵盤中斷時調用pytest_exception_interact(node, call, report)
拋出異常時調用pytest_enter_pdb(config)
進入pdb之前調用
hook 聲明周期示意圖:
應用示例:
比如需要收集用例錯誤信息,並在測試完成後存入資料庫。那麼就可以在pytest_runtest_logreport中保存錯誤信息至全局變量,並在pytest_sessionfinish中(測試完成後)將對應的信息保存至資料庫即可
最後
通過pytest強大的hook可以定製很多自己需要的功能,通用型的功能還可以製作為插件。
👆單擊連結直達、長按諮詢吳老師或公眾號內回復「2」
無論上課或自學,
你首先需要準備:
每天 2 小時+的學習時間,
每天堅持寫代碼的習慣!
有投入才有產出,
10k+的漲幅需要 1 年以上的努力!
祝你成功!
光榮之路出品
測試大佬和小白的故事
招聘QQ群:203715128