Skip to content

Commit cfdd5d2

Browse files
Show test docstrings as names in the HTML report (#143)
Add pytest hooks so the published test report uses each test's docstring as its display name (falling back to the node id where there is none), and titles the report "Rubie Gateway Tests". Makes the report readable for the assurance audience.
1 parent 6875434 commit cfdd5d2

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

tests/conftest.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import inspect
12
import shutil
23
import sys
34
from contextlib import contextmanager
@@ -15,6 +16,43 @@
1516
sys.path.append(f"{Path(__file__).parent.parent}/src")
1617

1718

19+
def pytest_html_report_title(report):
20+
report.title = "Rubie Gateway Tests"
21+
22+
23+
def _readable_test_name(item):
24+
"""
25+
Build a human-readable name for the test report from the test's
26+
docstring. Returns None when there is no docstring, so the default
27+
nodeid is kept.
28+
"""
29+
test_fn = getattr(item, "obj", None)
30+
docstring = inspect.getdoc(test_fn) if test_fn else None
31+
if not docstring:
32+
return None
33+
34+
# First non-empty line, whitespace collapsed — docstrings are often
35+
# multi-line and indented, which would render badly as a node id.
36+
first_line = next((line.strip() for line in docstring.splitlines() if line.strip()), "")
37+
if not first_line:
38+
return None
39+
40+
# Keep parametrised cases distinct (they share one docstring).
41+
param_id = getattr(getattr(item, "callspec", None), "id", None)
42+
return f"{first_line} [{param_id}]" if param_id else first_line
43+
44+
45+
@pytest.hookimpl(hookwrapper=True)
46+
def pytest_runtest_makereport(item, call):
47+
"""Use the test's docstring as its name in the HTML report, when present."""
48+
outcome = yield
49+
report = outcome.get_result()
50+
51+
readable_name = _readable_test_name(item)
52+
if readable_name:
53+
report.nodeid = readable_name
54+
55+
1856
@pytest.fixture(autouse=True)
1957
def mock_azure_credential():
2058
mock = MagicMock()

0 commit comments

Comments
 (0)