Skip to content

Commit 6fa7b16

Browse files
Merge pull request #2497 from pkch/firstresult
Docs: clarify when hooks stop after the first non-None result
2 parents f0541b6 + 4a992ba commit 6fa7b16

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

_pytest/hookspec.py

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,19 @@ def pytest_configure(config):
7373

7474
@hookspec(firstresult=True)
7575
def pytest_cmdline_parse(pluginmanager, args):
76-
"""return initialized config object, parsing the specified args. """
76+
"""return initialized config object, parsing the specified args.
77+
78+
Stops at first non-None result, see :ref:`firstresult` """
7779

7880
def pytest_cmdline_preparse(config, args):
7981
"""(deprecated) modify command line arguments before option parsing. """
8082

8183
@hookspec(firstresult=True)
8284
def pytest_cmdline_main(config):
8385
""" called for performing the main command line action. The default
84-
implementation will invoke the configure hooks and runtest_mainloop. """
86+
implementation will invoke the configure hooks and runtest_mainloop.
87+
88+
Stops at first non-None result, see :ref:`firstresult` """
8589

8690
def pytest_load_initial_conftests(early_config, parser, args):
8791
""" implements the loading of initial conftest files ahead
@@ -94,7 +98,9 @@ def pytest_load_initial_conftests(early_config, parser, args):
9498

9599
@hookspec(firstresult=True)
96100
def pytest_collection(session):
97-
""" perform the collection protocol for the given session. """
101+
""" perform the collection protocol for the given session.
102+
103+
Stops at first non-None result, see :ref:`firstresult` """
98104

99105
def pytest_collection_modifyitems(session, config, items):
100106
""" called after collection has been performed, may filter or re-order
@@ -108,11 +114,15 @@ def pytest_ignore_collect(path, config):
108114
""" return True to prevent considering this path for collection.
109115
This hook is consulted for all files and directories prior to calling
110116
more specific hooks.
117+
118+
Stops at first non-None result, see :ref:`firstresult`
111119
"""
112120

113121
@hookspec(firstresult=True)
114122
def pytest_collect_directory(path, parent):
115-
""" called before traversing a directory for collection files. """
123+
""" called before traversing a directory for collection files.
124+
125+
Stops at first non-None result, see :ref:`firstresult` """
116126

117127
def pytest_collect_file(path, parent):
118128
""" return collection Node or None for the given path. Any new node
@@ -133,7 +143,9 @@ def pytest_deselected(items):
133143

134144
@hookspec(firstresult=True)
135145
def pytest_make_collect_report(collector):
136-
""" perform ``collector.collect()`` and return a CollectReport. """
146+
""" perform ``collector.collect()`` and return a CollectReport.
147+
148+
Stops at first non-None result, see :ref:`firstresult` """
137149

138150
# -------------------------------------------------------------------------
139151
# Python test function related hooks
@@ -145,15 +157,20 @@ def pytest_pycollect_makemodule(path, parent):
145157
This hook will be called for each matching test module path.
146158
The pytest_collect_file hook needs to be used if you want to
147159
create test modules for files that do not match as a test module.
148-
"""
160+
161+
Stops at first non-None result, see :ref:`firstresult` """
149162

150163
@hookspec(firstresult=True)
151164
def pytest_pycollect_makeitem(collector, name, obj):
152-
""" return custom item/collector for a python object in a module, or None. """
165+
""" return custom item/collector for a python object in a module, or None.
166+
167+
Stops at first non-None result, see :ref:`firstresult` """
153168

154169
@hookspec(firstresult=True)
155170
def pytest_pyfunc_call(pyfuncitem):
156-
""" call underlying test function. """
171+
""" call underlying test function.
172+
173+
Stops at first non-None result, see :ref:`firstresult` """
157174

158175
def pytest_generate_tests(metafunc):
159176
""" generate (multiple) parametrized calls to a test function."""
@@ -163,7 +180,8 @@ def pytest_make_parametrize_id(config, val, argname):
163180
"""Return a user-friendly string representation of the given ``val`` that will be used
164181
by @pytest.mark.parametrize calls. Return None if the hook doesn't know about ``val``.
165182
The parameter name is available as ``argname``, if required.
166-
"""
183+
184+
Stops at first non-None result, see :ref:`firstresult` """
167185

168186
# -------------------------------------------------------------------------
169187
# generic runtest related hooks
@@ -172,7 +190,9 @@ def pytest_make_parametrize_id(config, val, argname):
172190
@hookspec(firstresult=True)
173191
def pytest_runtestloop(session):
174192
""" called for performing the main runtest loop
175-
(after collection finished). """
193+
(after collection finished).
194+
195+
Stops at first non-None result, see :ref:`firstresult` """
176196

177197
def pytest_itemstart(item, node):
178198
""" (deprecated, use pytest_runtest_logstart). """
@@ -190,7 +210,9 @@ def pytest_runtest_protocol(item, nextitem):
190210
:py:func:`pytest_runtest_teardown`.
191211
192212
:return boolean: True if no further hook implementations should be invoked.
193-
"""
213+
214+
215+
Stops at first non-None result, see :ref:`firstresult` """
194216

195217
def pytest_runtest_logstart(nodeid, location):
196218
""" signal the start of running a single test item. """
@@ -215,7 +237,8 @@ def pytest_runtest_makereport(item, call):
215237
""" return a :py:class:`_pytest.runner.TestReport` object
216238
for the given :py:class:`pytest.Item <_pytest.main.Item>` and
217239
:py:class:`_pytest.runner.CallInfo`.
218-
"""
240+
241+
Stops at first non-None result, see :ref:`firstresult` """
219242

220243
def pytest_runtest_logreport(report):
221244
""" process a test setup/call/teardown report relating to
@@ -227,7 +250,9 @@ def pytest_runtest_logreport(report):
227250

228251
@hookspec(firstresult=True)
229252
def pytest_fixture_setup(fixturedef, request):
230-
""" performs fixture setup execution. """
253+
""" performs fixture setup execution.
254+
255+
Stops at first non-None result, see :ref:`firstresult` """
231256

232257
def pytest_fixture_post_finalizer(fixturedef):
233258
""" called after fixture teardown, but before the cache is cleared so
@@ -277,7 +302,9 @@ def pytest_report_header(config, startdir):
277302

278303
@hookspec(firstresult=True)
279304
def pytest_report_teststatus(report):
280-
""" return result-category, shortletter and verbose word for reporting."""
305+
""" return result-category, shortletter and verbose word for reporting.
306+
307+
Stops at first non-None result, see :ref:`firstresult` """
281308

282309
def pytest_terminal_summary(terminalreporter, exitstatus):
283310
""" add additional section in terminal summary reporting. """
@@ -295,7 +322,9 @@ def pytest_logwarning(message, code, nodeid, fslocation):
295322

296323
@hookspec(firstresult=True)
297324
def pytest_doctest_prepare_content(content):
298-
""" return processed content for a given doctest"""
325+
""" return processed content for a given doctest
326+
327+
Stops at first non-None result, see :ref:`firstresult` """
299328

300329
# -------------------------------------------------------------------------
301330
# error handling and internal debugging hooks

changelog/2493.doc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Explicitly state for which hooks the calls stop after the first non-None result.

doc/en/writing_plugins.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,11 @@ if ``myapp.testsupport.myplugin`` also declares ``pytest_plugins``, the contents
255255
of the variable will also be loaded as plugins, and so on.
256256

257257
This mechanism makes it easy to share fixtures within applications or even
258-
external applications without the need to create external plugins using
258+
external applications without the need to create external plugins using
259259
the ``setuptools``'s entry point technique.
260260

261261
Plugins imported by ``pytest_plugins`` will also automatically be marked
262-
for assertion rewriting (see :func:`pytest.register_assert_rewrite`).
262+
for assertion rewriting (see :func:`pytest.register_assert_rewrite`).
263263
However for this to have any effect the module must not be
264264
imported already; if it was already imported at the time the
265265
``pytest_plugins`` statement is processed, a warning will result and
@@ -357,6 +357,8 @@ allowed to raise exceptions. Doing so will break the pytest run.
357357

358358

359359

360+
.. _firstresult:
361+
360362
firstresult: stop at first non-None result
361363
-------------------------------------------
362364

0 commit comments

Comments
 (0)