Skip to content

Commit b68b80a

Browse files
committed
Add new pytest_runtest_logfinish hook
Fix #3101
1 parent 93306f6 commit b68b80a

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

_pytest/hookspec.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,25 @@ def pytest_runtest_protocol(item, nextitem):
304304

305305

306306
def pytest_runtest_logstart(nodeid, location):
307-
""" signal the start of running a single test item. """
307+
""" signal the start of running a single test item.
308+
309+
This hook will be called **before** :func:`pytest_runtest_setup`, :func:`pytest_runtest_call` and
310+
:func:`pytest_runtest_teardown` hooks.
311+
312+
:param str nodeid: full id of the item
313+
:param location: a triple of ``(filename, linenum, testname)``
314+
"""
315+
316+
317+
def pytest_runtest_logfinish(nodeid, location):
318+
""" signal the complete finish of running a single test item.
319+
320+
This hook will be called **after** :func:`pytest_runtest_setup`, :func:`pytest_runtest_call` and
321+
:func:`pytest_runtest_teardown` hooks.
322+
323+
:param str nodeid: full id of the item
324+
:param location: a triple of ``(filename, linenum, testname)``
325+
"""
308326

309327

310328
def pytest_runtest_setup(item):
@@ -445,7 +463,7 @@ def pytest_terminal_summary(terminalreporter, exitstatus):
445463
def pytest_logwarning(message, code, nodeid, fslocation):
446464
""" process a warning specified by a message, a code string,
447465
a nodeid and fslocation (both of which may be None
448-
if the warning is not tied to a partilar node/location)."""
466+
if the warning is not tied to a particular node/location)."""
449467

450468
# -------------------------------------------------------------------------
451469
# doctest hooks

_pytest/runner.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ def pytest_runtest_protocol(item, nextitem):
6060
nodeid=item.nodeid, location=item.location,
6161
)
6262
runtestprotocol(item, nextitem=nextitem)
63+
item.ihook.pytest_runtest_logfinish(
64+
nodeid=item.nodeid, location=item.location,
65+
)
6366
return True
6467

6568

changelog/3101.feature

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
New `pytest_runtest_logfinish <https://docs.pytest.org/en/latest/writing_plugins.html#_pytest.hookspec.pytest_runtest_logfinish>`_
2+
hook which is called when a test item has finished executing, analogous to
3+
`pytest_runtest_logstart <https://docs.pytest.org/en/latest/writing_plugins.html#_pytest.hookspec.pytest_runtest_start>`_.

doc/en/writing_plugins.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,8 @@ All runtest related hooks receive a :py:class:`pytest.Item <_pytest.main.Item>`
598598

599599
.. autofunction:: pytest_runtestloop
600600
.. autofunction:: pytest_runtest_protocol
601+
.. autofunction:: pytest_runtest_logstart
602+
.. autofunction:: pytest_runtest_logfinish
601603
.. autofunction:: pytest_runtest_setup
602604
.. autofunction:: pytest_runtest_call
603605
.. autofunction:: pytest_runtest_teardown

testing/test_runner.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,18 @@ def teardown_function(func):
202202
""")
203203
assert rec.ret == 1
204204

205+
def test_logstart_logfinish_hooks(self, testdir):
206+
rec = testdir.inline_runsource("""
207+
import pytest
208+
def test_func():
209+
pass
210+
""")
211+
reps = rec.getcalls("pytest_runtest_logstart pytest_runtest_logfinish")
212+
assert [x._name for x in reps] == ['pytest_runtest_logstart', 'pytest_runtest_logfinish']
213+
for rep in reps:
214+
assert rep.nodeid == 'test_logstart_logfinish_hooks.py::test_func'
215+
assert rep.location == ('test_logstart_logfinish_hooks.py', 1, 'test_func')
216+
205217
def test_exact_teardown_issue90(self, testdir):
206218
rec = testdir.inline_runsource("""
207219
import pytest

0 commit comments

Comments
 (0)