Skip to content

Commit 014ec83

Browse files
chapelluLudovic CHAPELET
authored andcommitted
feat: add examples tag to scenario tags
1 parent 01c99a6 commit 014ec83

File tree

3 files changed

+316
-50
lines changed

3 files changed

+316
-50
lines changed

src/pytest_bdd/parser.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ def render(self, context: Mapping[str, object]) -> Scenario:
208208
Returns:
209209
Scenario: A Scenario object with steps rendered based on the context.
210210
"""
211+
example = self.find_scenario_example_from_context(context)
211212
base_steps = self.all_background_steps + self._steps
212213
scenario_steps = [
213214
Step(
@@ -227,11 +228,18 @@ def render(self, context: Mapping[str, object]) -> Scenario:
227228
name=render_string(self.name, context),
228229
line_number=self.line_number,
229230
steps=scenario_steps,
230-
tags=self.tags,
231+
tags=self.tags | example.tags if example else self.tags,
231232
description=self.description,
232233
rule=self.rule,
233234
)
234235

236+
def find_scenario_example_from_context(self, context: Mapping[str, object]) -> Examples | None:
237+
for example in self.examples:
238+
example_param = dict(zip(example.example_params, example.examples[0]))
239+
if example_param == context:
240+
return example
241+
return None
242+
235243

236244
@dataclass(eq=False)
237245
class Scenario:

tests/parser/test.feature

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Feature: User login
99
# Background steps run before each scenario
1010
Given the login page is open
1111

12-
# Scenario within the rule
12+
# Scenario within the rule
1313
Scenario: Successful login with valid credentials
1414
Given the user enters a valid username
1515
And the user enters a valid password
@@ -22,7 +22,7 @@ Feature: User login
2222
When the user clicks the login button
2323
Then the user should see an error message "<error_message>"
2424

25-
# Examples table provides data for the scenario outline
25+
# Examples table provides data for the scenario outline
2626
Examples:
2727
| username | password | error_message |
2828
| invalidUser | wrongPass | Invalid username or password |
@@ -83,15 +83,32 @@ Feature: User login
8383
Please check your username and password and try again.
8484
If the problem persists, contact support.
8585
"""
86+
# Tags can also be used on exemples
87+
@scenario_tag
88+
Scenario Outline: Test tags on Examples
89+
Given the user enters "<username>" as username
90+
And the user enters "<password>" as password
91+
When the user clicks the login button
92+
Then the user should see an error message "<error_message>"
93+
94+
@example_tag_1
95+
Examples:
96+
| username | password | error_message |
97+
| invalidUser | wrongPass | Invalid username or password |
98+
99+
@example_tag_2
100+
Examples:
101+
| username | password | error_message |
102+
| user123 | incorrect | Invalid username or password |
86103

87104
@some-tag
88105
Rule: a sale cannot happen if there is no stock
89-
# Unhappy path
90-
Example: No chocolates left
91-
Given the customer has 100 cents
92-
And there are no chocolate bars in stock
93-
When the customer tries to buy a 1 cent chocolate bar
94-
Then the sale should not happen
106+
# Unhappy path
107+
Example: No chocolates left
108+
Given the customer has 100 cents
109+
And there are no chocolate bars in stock
110+
When the customer tries to buy a 1 cent chocolate bar
111+
Then the sale should not happen
95112

96113
Rule: A sale cannot happen if the customer does not have enough money
97114
# Unhappy path

0 commit comments

Comments
 (0)