Skip to content

Commit cb06bc7

Browse files
authored
Merge pull request #7017 from bluetech/doc-pytest_report_teststatus
Document the pytest_report_teststatus hook better and test uncovered functionality
2 parents e01dcbf + 1ce30fd commit cb06bc7

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

src/_pytest/hookspec.py

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
""" hook specifications for pytest plugins, invoked from main.py and builtin plugins. """
22
from typing import Any
3+
from typing import Mapping
34
from typing import Optional
5+
from typing import Tuple
6+
from typing import Union
47

58
from pluggy import HookspecMarker
69

710
from .deprecated import COLLECT_DIRECTORY_HOOK
811
from _pytest.compat import TYPE_CHECKING
912

1013
if TYPE_CHECKING:
14+
from _pytest.config import Config
1115
from _pytest.main import Session
16+
from _pytest.reports import BaseReport
1217

1318

1419
hookspec = HookspecMarker("pytest")
@@ -546,12 +551,32 @@ def pytest_report_collectionfinish(config, startdir, items):
546551

547552

548553
@hookspec(firstresult=True)
549-
def pytest_report_teststatus(report, config):
550-
""" return result-category, shortletter and verbose word for reporting.
554+
def pytest_report_teststatus(
555+
report: "BaseReport", config: "Config"
556+
) -> Tuple[
557+
str, str, Union[str, Mapping[str, bool]],
558+
]:
559+
"""Return result-category, shortletter and verbose word for status
560+
reporting.
551561
552-
:param _pytest.config.Config config: pytest config object
562+
The result-category is a category in which to count the result, for
563+
example "passed", "skipped", "error" or the empty string.
553564
554-
Stops at first non-None result, see :ref:`firstresult` """
565+
The shortletter is shown as testing progresses, for example ".", "s",
566+
"E" or the empty string.
567+
568+
The verbose word is shown as testing progresses in verbose mode, for
569+
example "PASSED", "SKIPPED", "ERROR" or the empty string.
570+
571+
pytest may style these implicitly according to the report outcome.
572+
To provide explicit styling, return a tuple for the verbose word,
573+
for example ``"rerun", "R", ("RERUN", {"yellow": True})``.
574+
575+
:param report: The report object whose status is to be returned.
576+
:param _pytest.config.Config config: The pytest config object.
577+
578+
Stops at first non-None result, see :ref:`firstresult`.
579+
"""
555580

556581

557582
def pytest_terminal_summary(terminalreporter, exitstatus, config):

testing/test_terminal.py

+23
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,29 @@ def test_rewrite(self, testdir, monkeypatch):
306306
tr.rewrite("hey", erase=True)
307307
assert f.getvalue() == "hello" + "\r" + "hey" + (6 * " ")
308308

309+
def test_report_teststatus_explicit_markup(
310+
self, testdir: Testdir, color_mapping
311+
) -> None:
312+
"""Test that TerminalReporter handles markup explicitly provided by
313+
a pytest_report_teststatus hook."""
314+
testdir.monkeypatch.setenv("PY_COLORS", "1")
315+
testdir.makeconftest(
316+
"""
317+
def pytest_report_teststatus(report):
318+
return 'foo', 'F', ('FOO', {'red': True})
319+
"""
320+
)
321+
testdir.makepyfile(
322+
"""
323+
def test_foobar():
324+
pass
325+
"""
326+
)
327+
result = testdir.runpytest("-v")
328+
result.stdout.fnmatch_lines(
329+
color_mapping.format_for_fnmatch(["*{red}FOO{reset}*"])
330+
)
331+
309332

310333
class TestCollectonly:
311334
def test_collectonly_basic(self, testdir):

0 commit comments

Comments
 (0)