Skip to content

Commit 5be56e2

Browse files
committed
Response to feedback
1 parent d5ce7dc commit 5be56e2

File tree

6 files changed

+32
-71
lines changed

6 files changed

+32
-71
lines changed

src/pytest_bdd/generation.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ def generate_code(features: list[Feature], scenarios: list[ScenarioTemplate], st
6262
"""Generate test code for the given filenames."""
6363
grouped_steps = group_steps(steps)
6464
template = template_lookup.get_template("test.py.mak")
65-
6665
code = template.render(
6766
features=features,
6867
scenarios=scenarios,
@@ -84,39 +83,42 @@ def show_missing_code(config: Config) -> int:
8483
def print_missing_code(scenarios: list[ScenarioTemplate], steps: list[Step]) -> None:
8584
"""Print missing code with TerminalWriter."""
8685
tw = TerminalWriter()
86+
scenario = step = None
8787

8888
for scenario in scenarios:
8989
tw.line()
9090
tw.line(
91-
f'Scenario "{scenario.name}" is not bound to any test in the feature "{scenario.feature.name}"'
92-
f" in the file {scenario.feature.filename}:{scenario.line_number}",
91+
'Scenario "{scenario.name}" is not bound to any test in the feature "{scenario.feature.name}"'
92+
" in the file {scenario.feature.filename}:{scenario.line_number}".format(scenario=scenario),
9393
red=True,
9494
)
9595

96-
tw.sep("-", red=True)
96+
if scenario:
97+
tw.sep("-", red=True)
9798

9899
for step in steps:
99100
tw.line()
100101
if step.scenario is not None:
101102
tw.line(
102-
f"""Step {step} is not defined in the scenario "{step.scenario.name}" in the feature"""
103-
f""" "{step.scenario.feature.name}" in the file"""
104-
f" {step.scenario.feature.filename}:{step.line_number}",
103+
"""Step {step} is not defined in the scenario "{step.scenario.name}" in the feature"""
104+
""" "{step.scenario.feature.name}" in the file"""
105+
""" {step.scenario.feature.filename}:{step.line_number}""".format(step=step),
105106
red=True,
106107
)
107108
elif step.background is not None:
108109
tw.line(
109-
f"""Step {step} is not defined in the background of the feature"""
110-
f""" "{step.background.feature.name}" in the file"""
111-
f" {step.background.feature.filename}:{step.line_number}",
110+
"""Step {step} is not defined in the background of the feature"""
111+
""" "{step.background.feature.name}" in the file"""
112+
""" {step.background.feature.filename}:{step.line_number}""".format(step=step),
112113
red=True,
113114
)
114115

115-
tw.sep("-", red=True)
116-
tw.line("Please place the code above into the test file(s):")
116+
if step:
117+
tw.sep("-", red=True)
118+
119+
tw.line("Please place the code above to the test file(s):")
117120
tw.line()
118121

119-
# Group features and generate the test code
120122
features = sorted(
121123
(scenario.feature for scenario in scenarios), key=lambda feature: feature.name or feature.filename
122124
)

src/pytest_bdd/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class ScenarioTemplate:
137137
description: str | None = None
138138
tags: set[str] = field(default_factory=set)
139139
_steps: list[Step] = field(init=False, default_factory=list)
140-
examples: list[Examples] = field(default_factory=list)
140+
examples: list[Examples] | None = field(default_factory=list[Examples])
141141

142142
def add_step(self, step: Step) -> None:
143143
"""Add a step to the scenario.

src/pytest_bdd/scenario.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
import logging
1818
import os
1919
import re
20+
import warnings
2021
from collections.abc import Iterable, Iterator
2122
from typing import TYPE_CHECKING, Any, Callable, TypeVar, cast
2223

2324
import pytest
2425
from _pytest.fixtures import FixtureDef, FixtureManager, FixtureRequest, call_fixture_func
25-
from pytest import PytestUnknownMarkWarning, warns
26+
from pytest import PytestUnknownMarkWarning
2627
from typing_extensions import ParamSpec
2728

2829
from . import exceptions
@@ -305,23 +306,20 @@ def collect_example_parametrizations(
305306
templated_scenario: ScenarioTemplate,
306307
) -> list[ParameterSet] | None:
307308
parametrizations = []
308-
has_multiple_examples = len(templated_scenario.examples) > 1
309309

310-
for example_id, examples in enumerate(templated_scenario.examples):
311-
_tags: set = examples.tags or set()
312-
example_marks = []
313-
if _tags:
314-
with warns(PytestUnknownMarkWarning, match=r"Unknown pytest\.mark\.\w+"):
315-
example_marks = [pytest.mark.__getattr__(tag) for tag in _tags]
310+
for examples in templated_scenario.examples:
311+
tags: set = examples.tags or set()
312+
313+
with warnings.catch_warnings():
314+
warnings.filterwarnings("ignore", category=PytestUnknownMarkWarning)
315+
example_marks = [getattr(pytest.mark, tag) for tag in tags]
316316

317317
for context in examples.as_contexts() or [{}]:
318-
test_id = (
319-
"-".join((str(example_id), *context.values())) if has_multiple_examples else "-".join(context.values())
320-
)
318+
param_id = "-".join(context.values())
321319
parametrizations.append(
322320
pytest.param(
323321
context,
324-
id=test_id,
322+
id=param_id,
325323
marks=example_marks,
326324
),
327325
)

tests/feature/test_outline.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ def test_multiple_outlined(pytester):
127127
1, 4.0, "-3",
128128
]
129129
# fmt: on
130+
result = pytester.runpytest("-k", "positive", "-vv")
131+
result.assert_outcomes(passed=2, deselected=2)
132+
133+
result = pytester.runpytest("-k", "positive or negative", "-vv")
134+
result.assert_outcomes(passed=4, deselected=0)
130135

131136

132137
def test_unused_params(pytester):

tests/feature/test_tags.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -226,47 +226,3 @@ def _():
226226

227227
result = pytester.runpytest("-m", "tag2", "-vv")
228228
result.assert_outcomes(passed=1, deselected=1)
229-
230-
231-
def test_tags_against_multiple_examples_tables(pytester):
232-
pytester.makefile(
233-
".feature",
234-
test="""\
235-
Feature: Scenario with tags over multiple lines
236-
237-
Scenario Outline: Tags
238-
Given I have a <item>
239-
240-
@food
241-
Examples: Food
242-
| item |
243-
| bun |
244-
| ice |
245-
246-
@drink
247-
Examples: Drinks
248-
| item |
249-
| water |
250-
| juice |
251-
""",
252-
)
253-
pytester.makepyfile(
254-
"""
255-
from pytest_bdd import given, scenarios, parsers
256-
257-
scenarios('test.feature')
258-
259-
@given(parsers.parse('I have a {item}'))
260-
def _(item: str):
261-
pass
262-
"""
263-
)
264-
265-
result = pytester.runpytest("-m", "food", "-vv")
266-
result.assert_outcomes(passed=2, deselected=2)
267-
268-
result = pytester.runpytest("-m", "drink", "-vv")
269-
result.assert_outcomes(passed=2, deselected=2)
270-
271-
result = pytester.runpytest("-m", "food or drink", "-vv")
272-
result.assert_outcomes(passed=4, deselected=0)

tests/generation/test_generate_missing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_missing_steps():
8484
['Step Given "I have a foobar" is not defined in the background of the feature "Missing code generation" *']
8585
)
8686

87-
result.stdout.fnmatch_lines(["Please place the code above into the test file(s):"])
87+
result.stdout.fnmatch_lines(["Please place the code above to the test file(s):"])
8888

8989

9090
def test_generate_missing_with_step_parsers(pytester):

0 commit comments

Comments
 (0)