From 0f4a5b88c9b2868c1f521be14465720611735a06 Mon Sep 17 00:00:00 2001 From: ryux1 Date: Sun, 6 Sep 2026 11:08:08 +0200 Subject: [PATCH] Display scenario tags in gherkin reporter --- CHANGES.rst | 1 + src/pytest_bdd/gherkin_terminal_reporter.py | 9 ++++++- .../feature/test_gherkin_terminal_reporter.py | 26 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 2706c8aeb..09f10144c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -11,6 +11,7 @@ Unreleased Added +++++ +* Gherkin terminal reporter output now includes scenario tags in verbose modes. `#476 `_ Changed +++++++ diff --git a/src/pytest_bdd/gherkin_terminal_reporter.py b/src/pytest_bdd/gherkin_terminal_reporter.py index e18719c17..20fbe1ce5 100644 --- a/src/pytest_bdd/gherkin_terminal_reporter.py +++ b/src/pytest_bdd/gherkin_terminal_reporter.py @@ -47,6 +47,10 @@ def __init__(self, config: Config) -> None: super().__init__(config) self.current_rule: str | None = None + def _write_tags(self, tags: list[str], indent: str) -> None: + if tags: + self._tw.write(f"{indent} (tags: {', '.join(tags)})\n") + def pytest_runtest_logreport(self, report: TestReport) -> None: rep = report res = self.config.hook.pytest_report_teststatus(report=rep, config=self.config) @@ -96,6 +100,7 @@ def pytest_runtest_logreport(self, report: TestReport) -> None: self._tw.write(" ") self._tw.write(word, **word_markup) self._tw.write("\n") + self._write_tags(scenario["tags"], indent) elif self.verbosity > 1: self.ensure_newline() self._tw.write(f"{scenario['feature']['keyword']}: ", **feature_markup) @@ -114,6 +119,8 @@ def pytest_runtest_logreport(self, report: TestReport) -> None: for step in scenario["steps"]: self._tw.write(f"{indent} {step['keyword']} {step['name']}\n", **scenario_markup) self._tw.write(f"{indent} {word}", **word_markup) - self._tw.write("\n\n") + self._tw.write("\n") + self._write_tags(scenario["tags"], indent) + self._tw.write("\n") self.stats.setdefault(cat, []).append(rep) diff --git a/tests/feature/test_gherkin_terminal_reporter.py b/tests/feature/test_gherkin_terminal_reporter.py index 024e88a70..55a9753d5 100644 --- a/tests/feature/test_gherkin_terminal_reporter.py +++ b/tests/feature/test_gherkin_terminal_reporter.py @@ -12,6 +12,15 @@ Then world explodes """ +FEATURE_WITH_TAGS = """\ +Feature: Gherkin terminal output feature + @R1.1 @R1.2 + Scenario: Scenario example 1 + Given there is a bar + When the bar is accessed + Then world explodes +""" + TEST = """\ from pytest_bdd import given, when, then, scenario @@ -97,6 +106,23 @@ def test_double_verbose_mode_should_display_full_scenario_description(pytester): result.stdout.fnmatch_lines("*PASSED") +@pytest.mark.parametrize("verbosity", ["-v", "-vv"]) +def test_verbose_modes_display_scenario_tags(pytester, verbosity): + pytester.makeini("""\ +[pytest] +markers = + R1.1 + R1.2 +""") + pytester.makefile(".feature", test=FEATURE_WITH_TAGS) + pytester.makepyfile(TEST) + + result = pytester.runpytest("--gherkin-terminal-reporter", verbosity) + + result.assert_outcomes(passed=1, failed=0) + result.stdout.fnmatch_lines("*(tags: R1.1, R1.2)") + + @pytest.mark.parametrize("verbosity", ["", "-v", "-vv"]) def test_error_message_for_missing_steps(pytester, verbosity): pytester.makefile(".feature", test=FEATURE)