|
| 1 | +import inspect |
1 | 2 | import shutil |
2 | 3 | import sys |
3 | 4 | from contextlib import contextmanager |
|
15 | 16 | sys.path.append(f"{Path(__file__).parent.parent}/src") |
16 | 17 |
|
17 | 18 |
|
| 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 | + |
18 | 56 | @pytest.fixture(autouse=True) |
19 | 57 | def mock_azure_credential(): |
20 | 58 | mock = MagicMock() |
|
0 commit comments