diff --git a/CHANGES.rst b/CHANGES.rst index 80af7bd17..a39ddb2a2 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -48,6 +48,7 @@ Removed Fixed +++++ +* ``--generate-missing`` no longer reports a defined background step as missing when the feature also has a scenario that is not bound to a test. Security ++++++++ diff --git a/src/pytest_bdd/generation.py b/src/pytest_bdd/generation.py index 69395c51d..902cee37f 100644 --- a/src/pytest_bdd/generation.py +++ b/src/pytest_bdd/generation.py @@ -156,7 +156,8 @@ def parse_feature_files( itertools.chain.from_iterable(feature.scenarios.values() for feature in features), key=lambda scenario: (scenario.feature.name or scenario.feature.filename, scenario.name), ) - steps = sorted((step for scenario in scenarios for step in scenario.steps), key=lambda step: step.name) + # Background steps are shared by every scenario of the feature (or rule), so keep each step object only once. + steps = sorted(dict.fromkeys(step for scenario in scenarios for step in scenario.steps), key=lambda step: step.name) return features, scenarios, steps diff --git a/tests/generation/test_generate_missing.py b/tests/generation/test_generate_missing.py index 898ea8dc0..c333440e4 100644 --- a/tests/generation/test_generate_missing.py +++ b/tests/generation/test_generate_missing.py @@ -89,6 +89,56 @@ def test_missing_steps(): result.stdout.fnmatch_lines(["Please place the code above to the test file(s):"]) +def test_generate_missing_defined_background_step_with_unbound_scenario(pytester): + """Test that a defined background step is not reported as missing when another scenario is not bound.""" + pytester.makefile( + ".feature", + generation=textwrap.dedent( + """\ + Feature: Missing code generation with a background + + Background: + Given I have a foobar + + Scenario: Bound scenario + Given I have a bar + + Scenario: Unbound scenario + Given I have a bar + """ + ), + ) + + pytester.makepyfile( + textwrap.dedent( + """\ + from pytest_bdd import scenario, given + + @given("I have a foobar") + def _(): + return "foobar" + + @given("I have a bar") + def _(): + return "bar" + + @scenario("generation.feature", "Bound scenario") + def test_bound(): + pass + """ + ) + ) + + result = pytester.runpytest("--generate-missing", "--feature", "generation.feature") + result.assert_outcomes(passed=0, failed=0, errors=0) + assert not result.stderr.str() + assert result.ret == 0 + + result.stdout.fnmatch_lines(['Scenario "Unbound scenario" is not bound to any test *']) + result.stdout.no_fnmatch_line("*Background step*is not defined*") + result.stdout.no_fnmatch_line("*@given('I have a foobar')*") + + def test_generate_missing_with_step_parsers(pytester): """Test that step parsers are correctly discovered and won't be part of the missing steps.""" pytester.makefile(