Coverage for pytest_beehave/steps_reporter.py: 100%
29 statements
« prev ^ index » next coverage.py v7.8.0, created at 2026-04-21 04:49 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2026-04-21 04:49 +0000
1"""Terminal steps reporter for pytest-beehave."""
3from __future__ import annotations
5import sys
7import pytest
10class StepsReporter:
11 """Prints BDD step docstrings to the terminal for tests/features/ tests."""
13 def __init__(self, config: pytest.Config) -> None:
14 """Initialise the reporter.
16 Args:
17 config: The pytest Config object.
18 """
19 self._config = config
21 def pytest_runtest_logreport(self, report: pytest.TestReport) -> None:
22 """Print steps docstring after each test call phase report.
24 Args:
25 report: The test report for the current phase.
26 """
27 if report.when != "call" and not (report.when == "setup" and report.skipped):
28 return
29 config = self._config
30 option = config.option
31 verbose = option.verbose
32 if verbose < 1:
33 return
34 nodeid = report.nodeid
35 if "tests/features/" not in nodeid:
36 return
37 docstring = getattr(report, "_beehave_docstring", "")
38 if not docstring:
39 return
40 self._write_steps(docstring.strip())
42 def _write_steps(self, steps: str) -> None:
43 """Write the steps string to the terminal or stdout fallback.
45 Args:
46 steps: The stripped docstring content to write.
47 """
48 try:
49 config = self._config
50 writer = config.get_terminal_writer()
51 writer.write("\n" + steps + "\n")
52 except (AssertionError, AttributeError):
53 sys.stdout.write("\n" + steps + "\n")
54 sys.stdout.flush()