pytest文档70-Hook钩子函数完整API总结

摘要:
初始化挂钩初始化挂钩需要插件和conftest.py文件pytest_Addoption(解析器):以允许通过calling_configure(config):pytest_aunconfigure(config):pytest_Sessionstart(session):collection集合挂钩pytest_collection(session)添加新的挂钩pytest:
前言

pytest 的钩子函数有很多,通过钩子函数的学习可以了解到pytest在执行用例的每个阶段做什么事情,也方便后续对pytest二次开发学习。
详细文档可以查看pytest官方文档https://docs.pytest.org/en/latest/reference.html#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()。

详细文档可以查看pytest官方文档https://docs.pytest.org/en/latest/reference.html#hooks

免责声明:文章转载自《pytest文档70-Hook钩子函数完整API总结》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Linux及文件系统基本介绍切线空间下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

windows和cisco两种创建DHCP服务器方法

1.启动【添加/删除程序】对话框 2.单击【添加/删除 Windows 组件】出现 【windows 组件向导单击下一步】出现 【Windows组件】对话框从列表中选择【网络服务】 3. 单击【详细内容】,从列表中选取【动态主机配置协议(DHCP)】如单击【确定】 4. 单击【下一步】 输入到 Windows2000 Server 的安装源文件的路径,单击...

Ueditor1.4.3.3+springMvc+maven 实现图片上传

前记:由于项目中需要有一个新增数据并且要能支持表格图片上传的功能。使用了ueditor控件。为实现这个功能,从一开始什么都看不懂,到一直连着弄了5天,总算是有了眉目。在此记录一下以便能帮到可以和我一样遇到问题的人!本人使用的是ueditor 1.4.3.3 的jsp 版本的。 首先下载ueditor 开发版 到本地目录 下载地址:http://uedito...

h5页面跳转小程序

2020年以前, 只能通过 web-view内嵌h5跳转小程序,现在  可以直接跳了!!!!!!  官方文档:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html 用的是JS-SDK的接口,需要使用到js-sdk-1.6.0的版本才有支持,htt...

webpack+javascript搭建项目环境大概流程和各个流程的功能作用

前言 本文是对项目开发中webpack+原生js搭建流程的步骤梳理,和常见配置项的功能讲解.并不适用于vue/react框架 因为实际项目上的配置代码过多,放在一章写太长了,怕看不下去,所以实际代码另写在这里,两篇文章可以搭配看:https://www.cnblogs.com/liuXiaoDi/p/12245421.html webpcak里配置的路径都...

JedisPool无法获得资源问题

线上碰到一个问题:redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool at redis.clients.util.Pool.getResource(Pool.java:22) 线上会相隔不定时的天数后出现一次Jedis...

Laravel应用性能调优

这次性能测试方案中用到的优化技巧主要基于 Laravel 框架本身及其提供的工具。 关闭应用debugapp.debug=false 缓存配置信息php artisan config:cache 缓存路由信息php artisan router:cache 类映射加载优化php artisan optimize 自动加载优化composer dumpaut...