主页

索引

模块索引

搜索页面

@pytest.hookimpl-测试钩子

  • @pytest.hookimpl 是 pytest 测试框架中用于实现插件机制的一个关键装饰器。它允许开发者编写自定义的插件,以扩展和定制 pytest 的功能。

  • 【基本概念】pytest.hookimpl 是 pytest 框架的一个核心模块,它基于 Python 的装饰器和钩子概念。插件作者可以使用此装饰器将自定义函数标记为特定钩子的实现。

  • 【工作原理】插件作者使用 @pytest.hookimpl 装饰器将自定义函数标记为某个钩子的实现。pytest 框架在测试运行过程中,会在特定的时机(如测试开始、测试结束等)触发相应的钩子调用。受影响的插件中的钩子实现将被调用,并执行相应的逻辑来扩展或定制 pytest 的行为。

  • https://pytest-with-eric.com/hooks/pytest-hooks/

Fixtures vs Hooks

Functionality:

Hooks are used to alter the framework’s behavior and react to various testing events.
Fixtures are used for setting up and tearing down test environments and states.

Invocation:

Hooks are invoked
    automatically by Pytest based on certain events in the test lifecycle.
Fixtures are invoked
    explicitly by naming them as parameters in tests or other fixtures,
        or by setting the autouse=True flag.

Scope and Impact:

Hooks have a more global impact, influencing the overall behaviour of the test suite.
Fixtures have a localized impact, managing the environment for
    specific tests or groups of tests, controlled by the scope parameter.

主页

索引

模块索引

搜索页面