diff --git a/src/pytest_bdd/gherkin_terminal_reporter.py b/src/pytest_bdd/gherkin_terminal_reporter.py index e18719c17..72beaacd4 100644 --- a/src/pytest_bdd/gherkin_terminal_reporter.py +++ b/src/pytest_bdd/gherkin_terminal_reporter.py @@ -78,6 +78,8 @@ def pytest_runtest_logreport(self, report: TestReport) -> None: rule = scenario.get("rule") indent = " " if rule else "" + tags = [tag.lstrip("@") for tag in scenario["tags"]] + tags_text = ", ".join(tags) if self.verbosity == 1: self.ensure_newline() @@ -96,6 +98,8 @@ def pytest_runtest_logreport(self, report: TestReport) -> None: self._tw.write(" ") self._tw.write(word, **word_markup) self._tw.write("\n") + if tags: + self._tw.write(f"{indent} (tags: {tags_text})\n", **scenario_markup) elif self.verbosity > 1: self.ensure_newline() self._tw.write(f"{scenario['feature']['keyword']}: ", **feature_markup) @@ -114,6 +118,10 @@ 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") + if tags: + self._tw.write("\n") + self._tw.write(f"{indent} (tags: {tags_text})\n\n", **scenario_markup) + else: + self._tw.write("\n\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 cd558b7ab..628aa9470 100644 --- a/tests/feature/test_gherkin_terminal_reporter.py +++ b/tests/feature/test_gherkin_terminal_reporter.py @@ -93,6 +93,52 @@ def test_double_verbose_mode_should_display_full_scenario_description(pytester): result.stdout.fnmatch_lines("*When the bar is accessed") result.stdout.fnmatch_lines("*Then world explodes") result.stdout.fnmatch_lines("*PASSED") + assert not any("(tags:" in line for line in result.stdout.lines) + + +@pytest.mark.parametrize("verbosity", ["-v", "-vv"]) +def test_verbose_mode_should_display_scenario_tags(pytester, verbosity): + pytester.makefile( + ".feature", + test=textwrap.dedent( + """\ + Feature: Tool for network testing + @R1.1 @R1.2 + Scenario: Send packages to socket + Given a service listening on a random socket + When tool sends packages to socket + Then packages are received in the service + """ + ), + ) + pytester.makepyfile( + textwrap.dedent( + """\ + from pytest_bdd import given, when, then, scenario + + + @given("a service listening on a random socket") + def _(): + pass + + @when("tool sends packages to socket") + def _(): + pass + + @then("packages are received in the service") + def _(): + pass + + @scenario("test.feature", "Send packages to socket") + def test_send_packages_to_socket(): + pass + """ + ) + ) + + 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"])