Skip to content

feat: add examples tag to scenario tags #771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pytest-bdd"
version = "8.1.0"
version = "8.1.1"
description = "BDD for pytest"
authors = [
{name="Oleg Pidsadnyi", email="[email protected]"},
Expand Down
10 changes: 9 additions & 1 deletion src/pytest_bdd/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def render(self, context: Mapping[str, object]) -> Scenario:
Returns:
Scenario: A Scenario object with steps rendered based on the context.
"""
example = self.find_scenario_example_from_context(context)
base_steps = self.all_background_steps + self._steps
scenario_steps = [
Step(
Expand All @@ -227,11 +228,18 @@ def render(self, context: Mapping[str, object]) -> Scenario:
name=render_string(self.name, context),
line_number=self.line_number,
steps=scenario_steps,
tags=self.tags,
tags=self.tags | example.tags if example else self.tags,
description=self.description,
rule=self.rule,
)

def find_scenario_example_from_context(self, context: Mapping[str, object]) -> Examples | None:
for example in self.examples:
example_param = dict(zip(example.example_params, example.examples[0]))
if example_param == context:
return example
return None


@dataclass(eq=False)
class Scenario:
Expand Down
33 changes: 25 additions & 8 deletions tests/parser/test.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Feature: User login
# Background steps run before each scenario
Given the login page is open

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

# Examples table provides data for the scenario outline
# Examples table provides data for the scenario outline
Examples:
| username | password | error_message |
| invalidUser | wrongPass | Invalid username or password |
Expand Down Expand Up @@ -83,15 +83,32 @@ Feature: User login
Please check your username and password and try again.
If the problem persists, contact support.
"""
# Tags can also be used on exemples
@scenario_tag
Scenario Outline: Test tags on Examples
Given the user enters "<username>" as username
And the user enters "<password>" as password
When the user clicks the login button
Then the user should see an error message "<error_message>"

@example_tag_1
Examples:
| username | password | error_message |
| invalidUser | wrongPass | Invalid username or password |

@example_tag_2
Examples:
| username | password | error_message |
| user123 | incorrect | Invalid username or password |

@some-tag
Rule: a sale cannot happen if there is no stock
# Unhappy path
Example: No chocolates left
Given the customer has 100 cents
And there are no chocolate bars in stock
When the customer tries to buy a 1 cent chocolate bar
Then the sale should not happen
# Unhappy path
Example: No chocolates left
Given the customer has 100 cents
And there are no chocolate bars in stock
When the customer tries to buy a 1 cent chocolate bar
Then the sale should not happen

Rule: A sale cannot happen if the customer does not have enough money
# Unhappy path
Expand Down
38 changes: 0 additions & 38 deletions tests/parser/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,44 +34,6 @@ def test_multiple_features_error(pytester):
result.stdout.fnmatch_lines(["*FeatureError: Multiple features are not allowed in a single feature file.*"])


def test_step_outside_scenario_or_background_error(pytester):
"""Test step outside of a Scenario or Background."""
features = pytester.mkdir("features")
features.joinpath("test.feature").write_text(
textwrap.dedent(
"""
Feature: Invalid Feature
# Step not inside a scenario or background
Given a step that is not inside a scenario or background

Scenario: A valid scenario
Given a step inside a scenario

"""
),
encoding="utf-8",
)

pytester.makepyfile(
textwrap.dedent(
"""
from pytest_bdd import scenarios, given

@given("a step inside a scenario")
def step_inside_scenario():
pass

scenarios('features')
"""
)
)

result = pytester.runpytest()

# Expect the FeatureError for the step outside of scenario or background
result.stdout.fnmatch_lines(["*FeatureError: Step definition outside of a Scenario or a Background.*"])


def test_multiple_backgrounds_error(pytester):
"""Test multiple backgrounds in a single feature."""
features = pytester.mkdir("features")
Expand Down
Loading