Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
++++++++
Expand Down
3 changes: 2 additions & 1 deletion src/pytest_bdd/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
50 changes: 50 additions & 0 deletions tests/generation/test_generate_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down