From 84569415c6d6be1c7502b2ac7e388a5119b96113 Mon Sep 17 00:00:00 2001 From: Jason Allen Date: Fri, 29 Nov 2024 19:16:51 +0000 Subject: [PATCH 1/5] Add test to assert that issue 177 is resolved. Resolves #177 --- tests/feature/test_outline.py | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/feature/test_outline.py b/tests/feature/test_outline.py index e87a332..1006447 100644 --- a/tests/feature/test_outline.py +++ b/tests/feature/test_outline.py @@ -320,3 +320,63 @@ def test_forward_slash_in_params(pytester): result = pytester.runpytest("-s") result.assert_outcomes(passed=1) assert collect_dumped_objects(result) == ["https://my-site.com"] + + +def test_variable_reuse(pytester): + """Test example parameter reuse.""" + + pytester.makefile( + ".feature", + outline=textwrap.dedent( + """\ + Feature: Example parameters reuse + Scenario Outline: Check for example parameter re-use + Given some exists + When I print + And I output "some value" + And I echo + Then finish testing + + Examples: + | key | css_id | + | foo | bar | + | foo2 | bar2 | + + """ + ), + ) + + pytester.makepyfile( + textwrap.dedent( + """\ + from pytest_bdd import given, when, then, parsers, scenarios + + scenarios('outline.feature') + + + @given(parsers.parse("some {key} exists")) + def some_key_exists(key): + print(f"some {key} exists") + + @when(parsers.parse('I print {css_id}')) + def css_id(css_id): + assert css_id in ('bar', 'bar2') + + @when(parsers.parse('I echo {css_id}')) + def echo_val(css_id): + assert css_id in ('bar', 'bar2') + + @when(parsers.re('I output "(?P.+)"')) + def i_output(css_id): + assert css_id == 'some value' + + @then('finish testing') + def i_output(): + print("finished") + assert True + + """ + ) + ) + result = pytester.runpytest("-s") + result.assert_outcomes(passed=2) From c7189fd48fef7e8aaa79c9b19995561eec31223b Mon Sep 17 00:00:00 2001 From: jsa34 <31512041+jsa34@users.noreply.github.com> Date: Sun, 1 Dec 2024 17:07:15 +0000 Subject: [PATCH 2/5] Make clearer what the test is asserting --- tests/feature/test_outline.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/feature/test_outline.py b/tests/feature/test_outline.py index 1006447..58379cb 100644 --- a/tests/feature/test_outline.py +++ b/tests/feature/test_outline.py @@ -323,7 +323,10 @@ def test_forward_slash_in_params(pytester): def test_variable_reuse(pytester): - """Test example parameter reuse.""" + """ + Test same example parameter name used for step args between calls + doesn't redefine example arg value. + """ pytester.makefile( ".feature", @@ -358,18 +361,22 @@ def test_variable_reuse(pytester): def some_key_exists(key): print(f"some {key} exists") + @when(parsers.parse('I print {css_id}')) def css_id(css_id): assert css_id in ('bar', 'bar2') + @when(parsers.parse('I echo {css_id}')) def echo_val(css_id): assert css_id in ('bar', 'bar2') + @when(parsers.re('I output "(?P.+)"')) def i_output(css_id): assert css_id == 'some value' + @then('finish testing') def i_output(): print("finished") From 781797c9dcad4de22735b03d2f99b50c61752314 Mon Sep 17 00:00:00 2001 From: jsa34 <31512041+jsa34@users.noreply.github.com> Date: Sun, 1 Dec 2024 17:11:12 +0000 Subject: [PATCH 3/5] Make test purpose wording clearer --- tests/feature/test_outline.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/feature/test_outline.py b/tests/feature/test_outline.py index 58379cb..eff3696 100644 --- a/tests/feature/test_outline.py +++ b/tests/feature/test_outline.py @@ -324,8 +324,8 @@ def test_forward_slash_in_params(pytester): def test_variable_reuse(pytester): """ - Test same example parameter name used for step args between calls - doesn't redefine example arg value. + Test example parameter name and step arg do not redefine each other's value + if the same name is used for both in different steps. """ pytester.makefile( From 429205e5b5c98cd601bfa70397808cd29321e8e5 Mon Sep 17 00:00:00 2001 From: Jason Allen Date: Mon, 2 Dec 2024 18:48:10 +0000 Subject: [PATCH 4/5] Make test more focussed on the issue --- tests/feature/test_outline.py | 46 +++++++++++++---------------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/tests/feature/test_outline.py b/tests/feature/test_outline.py index eff3696..6a38f15 100644 --- a/tests/feature/test_outline.py +++ b/tests/feature/test_outline.py @@ -334,16 +334,13 @@ def test_variable_reuse(pytester): """\ Feature: Example parameters reuse Scenario Outline: Check for example parameter re-use - Given some exists - When I print - And I output "some value" - And I echo - Then finish testing + Given I get + When I set param to "other" + Then I check Examples: - | key | css_id | - | foo | bar | - | foo2 | bar2 | + | param | + | value | """ ), @@ -353,37 +350,28 @@ def test_variable_reuse(pytester): textwrap.dedent( """\ from pytest_bdd import given, when, then, parsers, scenarios + from pytest_bdd.utils import dump_obj scenarios('outline.feature') - @given(parsers.parse("some {key} exists")) - def some_key_exists(key): - print(f"some {key} exists") + @given(parsers.parse('I get {param}')) + def _(param): + dump_obj(("param1", param)) - @when(parsers.parse('I print {css_id}')) - def css_id(css_id): - assert css_id in ('bar', 'bar2') + @when(parsers.re('I set param to "(?P.+)"')) + def _(param): + dump_obj(("param2", param)) - @when(parsers.parse('I echo {css_id}')) - def echo_val(css_id): - assert css_id in ('bar', 'bar2') - - - @when(parsers.re('I output "(?P.+)"')) - def i_output(css_id): - assert css_id == 'some value' - - - @then('finish testing') - def i_output(): - print("finished") - assert True + @then(parsers.parse('I check {param}')) + def _(param): + dump_obj(("param3", param)) """ ) ) result = pytester.runpytest("-s") - result.assert_outcomes(passed=2) + result.assert_outcomes(passed=1) + assert collect_dumped_objects(result) == [("param1", "value"), ("param2", "other"), ("param3", "value")] From 6e1f54d2915e3e232a350ac0d319dd22d29b0619 Mon Sep 17 00:00:00 2001 From: Jason Allen Date: Mon, 2 Dec 2024 22:08:48 +0000 Subject: [PATCH 5/5] Update test steps --- tests/feature/test_outline.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/feature/test_outline.py b/tests/feature/test_outline.py index 6a38f15..fc5dd18 100644 --- a/tests/feature/test_outline.py +++ b/tests/feature/test_outline.py @@ -334,9 +334,9 @@ def test_variable_reuse(pytester): """\ Feature: Example parameters reuse Scenario Outline: Check for example parameter re-use - Given I get - When I set param to "other" - Then I check + Given the param is initially set from the example table as + When a step arg of the same name is set to "other" + Then the param is still set from the example table as Examples: | param | @@ -355,17 +355,17 @@ def test_variable_reuse(pytester): scenarios('outline.feature') - @given(parsers.parse('I get {param}')) + @given(parsers.parse('the param is initially set from the example table as {param}')) def _(param): dump_obj(("param1", param)) - @when(parsers.re('I set param to "(?P.+)"')) + @when(parsers.re('a step arg of the same name is set to "(?P.+)"')) def _(param): dump_obj(("param2", param)) - @then(parsers.parse('I check {param}')) + @then(parsers.parse('the param is still set from the example table as {param}')) def _(param): dump_obj(("param3", param))