From d3f47bf3468c964d4e41258c55db6577d99c7992 Mon Sep 17 00:00:00 2001 From: Kamran Ahmad <60498143+kamahmad@users.noreply.github.com> Date: Wed, 23 Sep 2020 19:45:55 +0530 Subject: [PATCH] Improved 'Declaring new hooks' section in docs. (#7782) Co-authored-by: Bruno Oliveira --- AUTHORS | 1 + doc/en/writing_plugins.rst | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/AUTHORS b/AUTHORS index b28e5613389..c8dfec4010a 100644 --- a/AUTHORS +++ b/AUTHORS @@ -151,6 +151,7 @@ Joshua Bronson Jurko Gospodnetić Justyna Janczyszyn Kale Kundert +Kamran Ahmad Karl O. Pinc Katarzyna Jachim Katarzyna Król diff --git a/doc/en/writing_plugins.rst b/doc/en/writing_plugins.rst index b9b46cb70b2..625ced7bd2f 100644 --- a/doc/en/writing_plugins.rst +++ b/doc/en/writing_plugins.rst @@ -614,6 +614,11 @@ among each other. Declaring new hooks ------------------------ +.. note:: + + This is a quick overview on how to add new hooks and how they work in general, but a more complete + overview can be found in `the pluggy documentation `__. + .. currentmodule:: _pytest.hookspec Plugins and ``conftest.py`` files may declare new hooks that can then be @@ -627,7 +632,7 @@ Hooks are usually declared as do-nothing functions that contain only documentation describing when the hook will be called and what return values are expected. The names of the functions must start with `pytest_` otherwise pytest won't recognize them. -Here's an example. Let's assume this code is in the ``hooks.py`` module. +Here's an example. Let's assume this code is in the ``sample_hook.py`` module. .. code-block:: python @@ -643,10 +648,10 @@ class or module can then be passed to the ``pluginmanager`` using the ``pytest_a .. code-block:: python def pytest_addhooks(pluginmanager): - """ This example assumes the hooks are grouped in the 'hooks' module. """ - from my_app.tests import hooks + """ This example assumes the hooks are grouped in the 'sample_hook' module. """ + from my_app.tests import sample_hook - pluginmanager.add_hookspecs(hooks) + pluginmanager.add_hookspecs(sample_hook) For a real world example, see `newhooks.py`_ from `xdist `_.