Skip to content

Pytest hooks

BetterPy treats pytest hook functions as first-class symbols: it generates hook stubs where pytest actually discovers them, links implementations back to the hook specification, and shows the hook documentation inline.

Generate a hook implementation

Generate pytest hook inserts a hook implementation into the file pytest loads hooks from, such as the nearest conftest.py or a plugin module. The generated function is opened as a live template, so the parameter list and body can be adjusted immediately after insertion.

# conftest.py
def pytest_collection_modifyitems(session, config, items):
    ...
  • A gutter marker next to a hook implementation leads to the hook specification and to other implementations of the same hook.
  • Hook navigation discovers implementations in files matched by the active pytest python_files patterns as well as conftest.py and plugin modules.
  • Go to Declaration on a hook implementation in conftest.py resolves to the corresponding hook specification shipped with pytest.
  • Quick documentation renders the hook's documented contract, including its parameters and expected return value.
  • Hook names are real references in conftest.py, so renaming and Find Usages cover implementations across conftest.py files.

Notes

  • Hook generation follows pytest's own locations, including conftest.py ancestry, pytest_plugins declarations, and plugin modules. Navigation and reference-based rename/Find Usages currently apply to conftest.py implementations.
  • The feature can be toggled in Settings | Tools | BetterPy | Testing (Pytest) | Fixtures.