Skip to content

Commit 1ff5df3

Browse files
committed
Apply the rendering to background steps, and test for these
1 parent e93097e commit 1ff5df3

File tree

2 files changed

+84
-18
lines changed

2 files changed

+84
-18
lines changed

src/pytest_bdd/parser.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ def render(self, context: Mapping[str, Any]) -> Scenario:
210210
Returns:
211211
Scenario: A Scenario object with steps rendered based on the context.
212212
"""
213+
base_steps = self.all_background_steps + self._steps
213214
scenario_steps = [
214215
Step(
215216
name=step.render_step_name(context),
@@ -220,15 +221,14 @@ def render(self, context: Mapping[str, Any]) -> Scenario:
220221
datatable=step.render_datatable(context),
221222
docstring=step.render_docstring(context),
222223
)
223-
for step in self._steps
224+
for step in base_steps
224225
]
225-
steps = self.all_background_steps + scenario_steps
226226
return Scenario(
227227
feature=self.feature,
228228
keyword=self.keyword,
229-
name=self.name,
229+
name=render_string(self.name, context),
230230
line_number=self.line_number,
231-
steps=steps,
231+
steps=scenario_steps,
232232
tags=self.tags,
233233
description=self.description,
234234
rule=self.rule,

tests/feature/test_scenario.py

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,32 +161,24 @@ def test_angular_brackets_are_not_parsed(pytester):
161161
"""
162162
pytester.makefile(
163163
".feature",
164-
simple='''
164+
simple="""
165165
Feature: Simple feature
166166
Scenario: Simple scenario
167167
Given I have a <tag>
168168
Then pass
169169
170170
Scenario Outline: Outlined scenario
171171
Given I have a templated <foo>
172-
When I have a templated datatable
173-
| <data> |
174-
| example |
175-
And I have a templated docstring
176-
"""
177-
This is a <doc>
178-
"""
179172
Then pass
180173
181174
Examples:
182-
| foo | data | doc |
183-
| bar | table | string |
184-
''',
175+
| foo |
176+
| bar |
177+
""",
185178
)
186179
pytester.makepyfile(
187180
"""
188-
from pytest_bdd import scenarios, given, when, then, parsers
189-
from pytest_bdd.utils import dump_obj
181+
from pytest_bdd import scenarios, given, then, parsers
190182
191183
scenarios("simple.feature")
192184
@@ -198,23 +190,97 @@ def _():
198190
def _(foo):
199191
return "foo"
200192
193+
@then("pass")
194+
def _():
195+
pass
196+
"""
197+
)
198+
result = pytester.runpytest()
199+
result.assert_outcomes(passed=2)
200+
201+
202+
def test_example_params(pytester):
203+
"""Test example params are rendered where necessary:
204+
* Step names
205+
* Docstring
206+
* Datatables
207+
"""
208+
pytester.makefile(
209+
".feature",
210+
example_params='''
211+
Feature: Example params
212+
Background:
213+
Given I have a background <background>
214+
And my background has:
215+
"""
216+
Background <background>
217+
"""
218+
219+
Scenario Outline: Outlined scenario
220+
Given I have a templated <foo>
221+
When I have a templated datatable
222+
| <data> |
223+
| example |
224+
And I have a templated docstring
225+
"""
226+
This is a <doc>
227+
"""
228+
Then pass
229+
230+
Examples:
231+
| background | foo | data | doc |
232+
| parameter | bar | table | string |
233+
''',
234+
)
235+
pytester.makepyfile(
236+
"""
237+
from pytest_bdd import scenarios, given, when, then, parsers
238+
from pytest_bdd.utils import dump_obj
239+
240+
scenarios("example_params.feature")
241+
242+
243+
@given(parsers.parse("I have a background {background}"))
244+
def _(background):
245+
return dump_obj(("background", background))
246+
247+
248+
@given(parsers.parse("I have a templated {foo}"))
249+
def _(foo):
250+
return "foo"
251+
252+
253+
@given("my background has:")
254+
def _(docstring):
255+
return dump_obj(("background_docstring", docstring))
256+
257+
258+
@given("I have a rule table:")
259+
def _(datatable):
260+
return dump_obj(("rule", datatable))
261+
262+
201263
@when("I have a templated datatable")
202264
def _(datatable):
203265
return dump_obj(("datatable", datatable))
204266
267+
205268
@when("I have a templated docstring")
206269
def _(docstring):
207270
return dump_obj(("docstring", docstring))
208271
272+
209273
@then("pass")
210274
def _():
211275
pass
212276
"""
213277
)
214278
result = pytester.runpytest("-s")
215-
result.assert_outcomes(passed=2)
279+
result.assert_outcomes(passed=1)
216280

217281
assert collect_dumped_objects(result) == [
282+
("background", "parameter"),
283+
("background_docstring", "Background parameter"),
218284
("datatable", [["table"], ["example"]]),
219285
("docstring", "This is a string"),
220286
]

0 commit comments

Comments
 (0)