pytest-Hook(钩子)函数

hook 函数相当于是 Pytest 的一些本地插件,Pytest 给我们提供了许多的 hook 函数,用于处理不同阶段的自定义行为。

有几个注意点:

  • hook 函数一般不建议写在非根目录下的 conftest 插件文件里面,我们一般是写在最外层的那个 conftest 里面。
  • hook 函数都是以 pytest_ 开头的函数。
  • 不同的 hook 函数有它自己的功能和所属的阶段。

1、hook 函数略览

Pytest 内置了许多的 hook 函数供我们使用,下面我们就按照阶段划分,罗列一下有哪些 hook 函数,可以大致感受一下:

引导钩子:

pytest_load_initial_conftests
pytest_cmdline_preparse
pytest_cmdline_parse
pytest_cmdline_main

初始化钩子:

pytest_addoption
pytest_addhooks
pytest_configure
pytest_unconfigure
pytest_sessionstart
pytest_sessionfinish
pytest_plugin_registered

收集钩子:

pytest_collection
pytest_collect_directory
pytest_collect_file
pytest_pycollect_makemodule
pytest_pycollect_makeitem
pytest_generate_tests
pytest_make_parametrize_id
pytest_collection_modifyitems
pytest_collection_finish

测试运行(runtest)钩子:

pytest_runtestloop
pytest_runtest_protocol
pytest_runtest_logstart
pytest_runtest_logfinish
pytest_runtest_setup
pytest_runtest_call
pytest_runtest_teardown
pytest_runtest_makereport
pytest_pyfunc_call

报告钩子:

pytest_collectstart
pytest_make_collect_report
pytest_itemcollected
pytest_collectreport
pytest_deselected
pytest_report_header
pytest_report_collectionfinish
pytest_report_teststatus 
pytest_terminal_summary
pytest_fixture_setup
pytest_fixture_post_finalizer
pytest_warning_recordedLiteral 
pytest_runtest_logreport
pytest_assertrepr_compare
pytest_assertion_pass

调试钩子:

pytest_internalerror
pytest_keyboard_interrupt
pytest_exception_interact
pytest_enter_pdb

这些钩子函数都是 Pytest 给我们提供的,你可以在 conftest 插件文件里面去重写函数来实现你的自定义功能。

看到这么多函数先别慌,咱不需要掌握所有的,因为很多是不常用的,也就是我开篇提到的 99% 的人都用不到的,其中经常使用到的一些比较常用的 hook 函数,将会在后续内容介绍到。

2、常用的 hook 函数

2.1、pytest_addoption

这个 hook 函数在前面将自定义命令行参数的时候已经用过了,它的用途就是注册命令行参数,这些值在测试运行开始时会被调用一次。

# conftest.py

def pytest_addoption(parser):
    parser.addoption()

前面有例子,这里就不多讲了,用法很简单。

它都是通过 parser.addoption 在定义命令行参数的。

2.2、pytest_configure

这个函数主要是用来获取命令行参数的:

# conftest.py

def pytest_configure(config):
    my_option = config.getoption("--opt")

是在执行测试执行运行,简单哈。

pytest_unconfigure 则是在执行测试退出之前运行。

2.3、pytest_sessionstart

这个函数在 session 对象创建之后,执行收集之前调用:

def pytest_sessionstart(session):
    """
    :param pytest.Session session: The pytest session object.
    """

session 对象里面有很多属性,常用的:

  • startdir:用例根目录的绝对路径。
  • items:用例对象的列表。
  • config:config对象。

你也可以往里面动态添加一些属性。

和它对应的 pytest_sessionfinish 是在所有测试结束,退出之前执行。

2.4、pytest_collection_modifyitems

这个函数主要用来调整用例:

# conftest.py

def pytest_collection_modifyitems(session, config, items):
    """
    :param pytest.Session session: The pytest session object.
    :param _pytest.config.Config config: The pytest config object.
    :param List[pytest.Item] items: List of item objects.
    """
  • session 为 Pytest 的 session 对象。
  • config 为 Pytest 的 config 对象。
  • items 是一个列表,其他每个元素就是一个用例对象。

item 里面有很多属性,常用的:

name:用例的名称
nodeid:从用例根目录开始到用例文件的路径
own_markers:用例的mark标签

pytest_collection_finish 则是在收集完并且修改完之后运行,它是在 pytest_collection_modifyitems 之后的。

2.5、pytest_runtest_setup

这个函数是在调用 setup 的时候运行:

def pytest_runtest_setup(item):
    ...

注意是 item,不是 items,item 是用例对象。

比如,你可以在每次用例执行之前输出用例的标题:

def pytest_runtest_setup(item):
    logger.info(item.function.__name__)

pytest_runtest_call 和 pytest_runtest_teardown 分别是在用例执行过程中和用例 teardown 阶段运行,用法是一样的。

2.6、pytest_runtest_makereport

这个函数是用于创建测试报告的,每个测试用例的测试报告都分为 setup、call 和 teardown 三个测试阶段。如果你熟悉 allure 报告的话,应该能轻易 get 到我说的。

def pytest_runtest_makereport(item):
    out = yield
    report = out.get_result()

欢迎关注公众号,与Joker一起探索测试之道。

参考链接:https://juejin.cn/post/7221769090834481189

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Anthony_路人甲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值