Skip to content

Commit 49cad00

Browse files
committed
Docs+Tests: clarify special discovery of pytest_generate_tests
- Document that pytest_generate_tests is also discovered in test modules/classes. - Clarify other hooks must live in conftest.py or plugins; add cross-links. - Add a tiny pytester test demonstrating the behavior. - Pure docs/tests; no behavior change.
1 parent 3d58e8d commit 49cad00

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

doc/en/how-to/parametrize.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ command line option and the parametrization of our test function:
240240
if "stringinput" in metafunc.fixturenames:
241241
metafunc.parametrize("stringinput", metafunc.config.getoption("stringinput"))
242242
243+
.. note::
244+
245+
The :hook:`pytest_generate_tests` hook can also be implemented directly in a test
246+
module or inside a test class; unlike other hooks, pytest will discover it there
247+
as well. Other hooks must live in a :ref:`conftest.py <localplugin>` or a plugin.
248+
See :ref:`writinghooks`.
249+
243250
If we now pass two stringinput values, our test will run twice:
244251

245252
.. code-block:: pytest

doc/en/how-to/writing_hook_functions.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ Example:
235235
"""
236236
print(config.hook)
237237
238+
.. note::
239+
240+
Unlike other hooks, the :hook:`pytest_generate_tests` hook is also discovered when
241+
defined inside a test module or test class. Other hooks must live in
242+
:ref:`conftest.py plugins <localplugin>` or external plugins.
243+
See :ref:`parametrize-basics` and the :ref:`hook-reference`.
238244

239245
.. _`addoptionhooks`:
240246

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def test_generate_tests_discovery_in_test_module_and_not_other_hooks(pytester):
2+
pytester.makepyfile(
3+
"""
4+
def pytest_generate_tests(metafunc):
5+
if "x" in metafunc.fixturenames:
6+
metafunc.parametrize("x", [1, 2])
7+
8+
def pytest_terminal_summary(terminalreporter):
9+
raise AssertionError("should not be called")
10+
11+
def test_x(x):
12+
assert x in (1, 2)
13+
"""
14+
)
15+
result = pytester.runpytest()
16+
result.assert_outcomes(passed=2)

0 commit comments

Comments
 (0)