Skip to content
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

Allow Examples table to include values not used in steps #391

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 3 additions & 3 deletions pytest_bdd/parser.py
Expand Up @@ -283,10 +283,10 @@ def validate(self):
"""
params = self.params
example_params = self.get_example_params()
if params and example_params and params != example_params:
if params and example_params and not params.issubset(example_params):
raise exceptions.ScenarioExamplesNotValidError(
"""Scenario "{0}" in the feature "{1}" has not valid examples. """
"""Set of step parameters {2} should match set of example values {3}.""".format(
"""Scenario "{0}" in the feature "{1}" does not have valid examples. """
"""Set of step parameters {2} should be a subset of example values {3}.""".format(
self.name, self.feature.filename, sorted(params), sorted(example_params)
)
)
Expand Down
56 changes: 49 additions & 7 deletions tests/feature/test_outline.py
Expand Up @@ -78,22 +78,22 @@ def test_outline(request):
result.assert_outcomes(passed=2)


def test_wrongly_outlined(testdir):
"""Test parametrized scenario when the test function lacks parameters."""
def test_outline_has_subset_of_parameters(testdir):
"""Test parametrized scenario when the test function has a subset of the parameters of the examples."""

testdir.makefile(
".feature",
outline=textwrap.dedent(
"""\
Feature: Outline
Scenario Outline: Outlined with wrong examples
Scenario Outline: Outlined with subset of examples
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers

Examples:
| start | eat | left | unknown_param |
| 12 | 5 | 7 | value |
| start | eat | left | notes |
| 12 | 5 | 7 | Should be ignored |

"""
),
Expand All @@ -105,18 +105,60 @@ def test_wrongly_outlined(testdir):
"""\
from pytest_bdd import scenario

@scenario("outline.feature", "Outlined with subset of examples",
example_converters=dict(start=int, eat=float, left=str))
def test_outline(request):
pass
"""
)
)
result = testdir.runpytest()
assert_outcomes(result, passed=1)


def test_wrongly_outlined_parameters_not_a_subset_of_examples(testdir):
"""Test parametrized scenario when the test function has a parameter set which is not a subset of those in the examples table."""

testdir.makefile(
".feature",
outline=textwrap.dedent(
"""\
Feature: Outline
Scenario Outline: Outlined with wrong examples
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers in my <right> bucket

Examples:
| start | eat | left |
| 12 | 5 | 7 |

"""
),
)
testdir.makeconftest(textwrap.dedent(STEPS))

testdir.makepyfile(
textwrap.dedent(
"""\
from pytest_bdd import scenario, then

@scenario("outline.feature", "Outlined with wrong examples")
def test_outline(request):
pass

@then(parsers.parse('I should have <left> cucumbers in my <right> bucket'))
def stepdef(left, right):
pass
"""
)
)
result = testdir.runpytest()
assert_outcomes(result, errors=1)
result.stdout.fnmatch_lines(
'*ScenarioExamplesNotValidError: Scenario "Outlined with wrong examples"*has not valid examples*',
'*ScenarioExamplesNotValidError: Scenario "Outlined with wrong examples"*does not have valid examples*',
)
result.stdout.fnmatch_lines("*should match set of example values [[]'eat', 'left', 'start', 'unknown_param'[]].*")
result.stdout.fnmatch_lines("*should be a subset of example values [[]'eat', 'left', 'start'[]].*")


def test_wrong_vertical_examples_scenario(testdir):
Expand Down