@@ -621,12 +621,39 @@ the new plugin:
621621
622622Hooks are usually declared as do-nothing functions that contain only
623623documentation describing when the hook will be called and what return values
624- are expected.
624+ are expected. The names of the functions must start with ` pytest_ ` otherwise pytest won't recognize them.
625625
626626For an example, see `newhooks.py `_ from `xdist <https://github.com/pytest-dev/pytest-xdist >`_.
627627
628628.. _`newhooks.py` : https://github.com/pytest-dev/pytest-xdist/blob/974bd566c599dc6a9ea291838c6f226197208b46/xdist/newhooks.py
629629
630+ To register the hooks with pytest they need to be structured in their own module or class. This
631+ class or module can then be passed to the `pluginmanager ` using the `pytest_addhooks ` function
632+ (which itself is a hook exposed by pytest):
633+
634+ .. code-block :: python
635+
636+ def pytest_addhooks (pluginmanager ):
637+ """ This example assumes the hooks are grouped in the pytest_hooks module. """
638+ from my_app.tests import pytest_hooks
639+ pluginmanager.add_hookspecs(pytest_hooks)
640+
641+ To execute hooks in one of your fixtures, use the `pytestconfig ` fixture. It holds all registered
642+ hooks under `hook ` by their name.
643+
644+ .. code-block :: python
645+
646+ @pytest.fixture ()
647+ def my_fixture (pytestconfig ):
648+ # call the hook called "pytest_my_hook"
649+ # `result` will be a list of return values from all registered functions.
650+ result = pytestconfig.hook.pytest_my_hook()
651+
652+ Now your hook is ready to be used. To register a function at the hook, other plugins or users must
653+ now simply define the function `pytest_my_hook ` with the correct signature in their `conftest.py `.
654+
655+ .. note ::
656+ If a hook uses parameters only keyword arguments can be used to invoke it.
630657
631658Optionally using hooks from 3rd party plugins
632659---------------------------------------------
0 commit comments