From 9cc09f76f2006c548b936f877f5d5d5afd0babb9 Mon Sep 17 00:00:00 2001 From: Jiri Kuncar Date: Wed, 15 Dec 2021 14:00:42 +0100 Subject: [PATCH 1/2] Ignore in step parameter values closes #447 --- pytest_bdd/parser.py | 4 ++++ tests/feature/test_steps.py | 18 +++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/pytest_bdd/parser.py b/pytest_bdd/parser.py index 8cbfbb14..236c74c4 100644 --- a/pytest_bdd/parser.py +++ b/pytest_bdd/parser.py @@ -357,8 +357,12 @@ def params(self): return tuple(frozenset(STEP_PARAM_RE.findall(self.name))) def render(self, context: typing.Mapping[str, typing.Any]): + example_params = set(self.scenario.examples.example_params) + def replacer(m: typing.Match): varname = m.group(1) + if varname not in example_params: + return m.group(0) return str(context[varname]) return STEP_PARAM_RE.sub(replacer, self.name) diff --git a/tests/feature/test_steps.py b/tests/feature/test_steps.py index 63ed1498..4c9a7fd8 100644 --- a/tests/feature/test_steps.py +++ b/tests/feature/test_steps.py @@ -11,12 +11,12 @@ def test_steps(testdir): are not mandatory in some cases. Scenario: Executed step by step - Given I have a foo fixture with value "foo" + Given I have a foo fixture with value "

foo

" And there is a list When I append 1 to the list And I append 2 to the list And I append 3 to the list - Then foo should have value "foo" + Then foo should have value "

foo

" But the list should be [1, 2, 3] """ ), @@ -25,15 +25,15 @@ def test_steps(testdir): testdir.makepyfile( textwrap.dedent( """\ - from pytest_bdd import given, when, then, scenario + from pytest_bdd import given, when, then, scenario, parsers @scenario("steps.feature", "Executed step by step") def test_steps(): pass - @given('I have a foo fixture with value "foo"', target_fixture="foo") - def foo(): - return "foo" + @given(parsers.parse('I have a foo fixture with value "{value}"'), target_fixture="foo") + def foo(value): + return value @given("there is a list", target_fixture="results") @@ -56,9 +56,9 @@ def append_3(results): results.append(3) - @then('foo should have value "foo"') - def foo_is_foo(foo): - assert foo == "foo" + @then(parsers.parse('foo should have value "{value}"')) + def foo_is_foo(foo, value): + assert foo == value @then("the list should be [1, 2, 3]") From 4ea54e3f379a02f27026310f56b4b56c105c5521 Mon Sep 17 00:00:00 2001 From: Jiri Kuncar Date: Wed, 15 Dec 2021 14:10:03 +0100 Subject: [PATCH 2/2] Collect all examples --- pytest_bdd/parser.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pytest_bdd/parser.py b/pytest_bdd/parser.py index 236c74c4..81ddec38 100644 --- a/pytest_bdd/parser.py +++ b/pytest_bdd/parser.py @@ -357,7 +357,11 @@ def params(self): return tuple(frozenset(STEP_PARAM_RE.findall(self.name))) def render(self, context: typing.Mapping[str, typing.Any]): - example_params = set(self.scenario.examples.example_params) + example_params = set() + if self.scenario: + example_params |= set(self.scenario.feature.examples.example_params) + if hasattr(self.scenario, "examples"): + example_params |= set(self.scenario.examples.example_params) def replacer(m: typing.Match): varname = m.group(1)