Skip to content

Commit ba66bb5

Browse files
committed
Feedback response
1 parent 5ebf4ea commit ba66bb5

File tree

3 files changed

+9
-34
lines changed

3 files changed

+9
-34
lines changed

src/pytest_bdd/generation.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from .compat import getfixturedefs
1313
from .feature import get_features
14-
from .parser import Feature, Rule, ScenarioTemplate, Step
14+
from .parser import Feature, ScenarioTemplate, Step
1515
from .scenario import inject_fixturedefs_for_step, make_python_docstring, make_python_name, make_string_literal
1616
from .steps import get_step_fixture_name
1717
from .types import STEP_TYPES
@@ -110,21 +110,7 @@ def print_missing_code(scenarios: list[ScenarioTemplate], steps: list[Step]) ->
110110
red=True,
111111
)
112112
elif step.background is not None:
113-
parent = step.background.parent
114-
115-
if isinstance(parent, Rule):
116-
parent_type = "rule"
117-
filename = parent.feature.filename
118-
elif isinstance(parent, Feature):
119-
parent_type = "feature"
120-
filename = parent.filename
121-
else:
122-
raise NotImplementedError(f"Unhandled parent type: {type(parent)}")
123-
124-
message = (
125-
f"Step {step} is not defined in the background of the {parent_type} "
126-
f'"{parent.name}" in the file {filename}:{step.line_number}'
127-
)
113+
message = f"Background step {step} is not defined."
128114
tw.line(message, red=True)
129115

130116
if step:

src/pytest_bdd/parser.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,7 @@ def steps(self) -> list[Step]:
169169
Returns:
170170
List[Step]: A list of steps, including any background steps from the feature.
171171
"""
172-
steps = []
173-
# Add all background steps
174-
steps.extend(self.all_background_steps)
175-
# Add the scenario's own steps
176-
steps.extend(self._steps)
177-
178-
return steps
172+
return self.all_background_steps + self._steps
179173

180174
def render(self, context: Mapping[str, Any]) -> Scenario:
181175
"""Render the scenario with the given context.
@@ -328,12 +322,10 @@ class Background:
328322
"""Represents the background steps for a feature.
329323
330324
Attributes:
331-
parent (Feature | Rule): The feature or rule to which this background belongs.
332325
line_number (int): The line number where the background starts in the file.
333326
steps (List[Step]): The list of steps in the background.
334327
"""
335328

336-
parent: Feature | Rule
337329
line_number: int
338330
steps: list[Step] = field(init=False, default_factory=list)
339331

@@ -455,9 +447,8 @@ def parse_scenario(
455447

456448
return scenario
457449

458-
def parse_background(self, background_data: GherkinBackground, parent: Feature | Rule) -> Background:
450+
def parse_background(self, background_data: GherkinBackground) -> Background:
459451
background = Background(
460-
parent=parent,
461452
line_number=background_data.location.line,
462453
)
463454
background.steps = self.parse_steps(background_data.steps)
@@ -492,7 +483,7 @@ def parse(self) -> Feature:
492483

493484
for child in feature_data.children:
494485
if child.background:
495-
feature.background = self.parse_background(child.background, feature)
486+
feature.background = self.parse_background(child.background)
496487
elif child.rule:
497488
self._parse_and_add_rule(child.rule, feature)
498489
elif child.scenario:
@@ -511,7 +502,7 @@ def _parse_and_add_rule(self, rule_data: GherkinRule, feature: Feature) -> None:
511502
)
512503

513504
# Add background if present within the rule
514-
background = self._extract_rule_background(rule_data, rule)
505+
background = self._extract_rule_background(rule_data)
515506
if background:
516507
rule.add_background(background)
517508

@@ -520,11 +511,11 @@ def _parse_and_add_rule(self, rule_data: GherkinRule, feature: Feature) -> None:
520511
scenario = self.parse_scenario(rule_scenario, feature, rule)
521512
feature.scenarios[scenario.name] = scenario
522513

523-
def _extract_rule_background(self, rule_data: GherkinRule, rule: Rule) -> Background | None:
514+
def _extract_rule_background(self, rule_data: GherkinRule) -> Background | None:
524515
"""Extract the first background from rule children if it exists."""
525516
for child in rule_data.children:
526517
if child.background:
527-
return self.parse_background(child.background, rule)
518+
return self.parse_background(child.background)
528519
return None
529520

530521
@staticmethod

tests/generation/test_generate_missing.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ def test_missing_steps():
8080
]
8181
)
8282

83-
result.stdout.fnmatch_lines(
84-
['Step Given "I have a foobar" is not defined in the background of the feature "Missing code generation" *']
85-
)
83+
result.stdout.fnmatch_lines(['Background step Given "I have a foobar" is not defined*'])
8684

8785
result.stdout.fnmatch_lines(["Please place the code above to the test file(s):"])
8886

0 commit comments

Comments
 (0)