44
55from pytest_bdd .utils import collect_dumped_objects
66
7+ STEPS = """\
8+ from pytest_bdd import given, when, then, parsers
9+ from pytest_bdd.utils import dump_obj
710
8- def test_scenario_with_empty_example_values (pytester ):
9- pytester .makefile (
10- ".feature" ,
11- outline = textwrap .dedent (
12- """\
13- Feature: Outline
14- Scenario Outline: Outlined with empty example values
15- Given there are <start> cucumbers
16- When I eat <eat> cucumbers
17- Then I should have <left> cucumbers
11+ # Using `parsers.re` so that we can match empty values
1812
19- Examples:
20- | start | eat | left |
21- | # | | |
22- """
23- ),
24- )
25- pytester .makeconftest (
26- textwrap .dedent (
27- """\
28- from pytest_bdd import given, when, then, parsers
29- from pytest_bdd.utils import dump_obj
13+ @given(parsers.re("there are (?P<start>.*?) cucumbers"))
14+ def _(start):
15+ dump_obj(start)
3016
31- # Using `parsers.re` so that we can match empty values
3217
33- @given (parsers.re("there are (?P<start >.*?) cucumbers"))
34- def _(start ):
35- dump_obj(start )
18+ @when (parsers.re("I eat (?P<eat >.*?) cucumbers"))
19+ def _(eat ):
20+ dump_obj(eat )
3621
3722
38- @when (parsers.re("I eat (?P<eat >.*?) cucumbers"))
39- def _(eat ):
40- dump_obj(eat )
23+ @then (parsers.re("I should have (?P<left >.*?) cucumbers"))
24+ def _(left ):
25+ dump_obj(left )
4126
27+ """
4228
43- @then(parsers.re("I should have (?P<left>.*?) cucumbers"))
44- def _(left):
45- dump_obj(left)
46-
47- """
48- )
49- )
5029
51- pytester .makepyfile (
52- textwrap .dedent (
53- """\
54- from pytest_bdd import scenario
55-
56- @scenario("outline.feature", "Outlined with empty example values")
57- def test_outline():
58- pass
59- """
60- )
61- )
62- result = pytester .runpytest ("-s" )
63- result .assert_outcomes (passed = 1 )
64- assert collect_dumped_objects (result ) == ["#" , "" , "" ]
65-
66-
67- def test_scenario_with_empty_example_values_none_transformer (pytester ):
68- """
69- Checks that `parsers.re` can transform empty values to None with a converter.
70- `parsers.parse` and `parsers.cfparse` won't work out of the box this way as they will fail to match the steps.
71- """
30+ def test_scenario_with_empty_example_values (pytester ):
7231 pytester .makefile (
7332 ".feature" ,
7433 outline = textwrap .dedent (
7534 """\
7635 Feature: Outline
77- Scenario Outline: Outlined with empty example values and transformer
36+ Scenario Outline: Outlined with empty example values
7837 Given there are <start> cucumbers
7938 When I eat <eat> cucumbers
8039 Then I should have <left> cucumbers
@@ -85,46 +44,21 @@ def test_scenario_with_empty_example_values_none_transformer(pytester):
8544 """
8645 ),
8746 )
88- pytester .makeconftest (
89- textwrap .dedent (
90- """\
91- from pytest_bdd import given, when, then, parsers
92- from pytest_bdd.utils import dump_obj
93-
94-
95- def empty_to_none(value):
96- return None if value.strip() == "" else value
97-
98-
99- @given(parsers.re("there are (?P<start>.*?) cucumbers"), converters={"start": empty_to_none})
100- def _(start):
101- dump_obj(start)
102-
103-
104- @when(parsers.re("I eat (?P<eat>.*?) cucumbers"), converters={"eat": empty_to_none})
105- def _(eat):
106- dump_obj(eat)
107-
108-
109- @then(parsers.re("I should have (?P<left>.*?) cucumbers"), converters={"left": empty_to_none})
110- def _(left):
111- dump_obj(left)
112-
113- """
114- )
115- )
47+ pytester .makeconftest (textwrap .dedent (STEPS ))
11648
11749 pytester .makepyfile (
11850 textwrap .dedent (
11951 """\
120- from pytest_bdd import scenario
52+ from pytest_bdd.utils import dump_obj
53+ from pytest_bdd import scenario
54+ import json
12155
122- @scenario("outline.feature", "Outlined with empty example values and transformer ")
123- def test_outline():
124- pass
125- """
56+ @scenario("outline.feature", "Outlined with empty example values")
57+ def test_outline():
58+ pass
59+ """
12660 )
12761 )
12862 result = pytester .runpytest ("-s" )
12963 result .assert_outcomes (passed = 1 )
130- assert collect_dumped_objects (result ) == ["#" , None , None ]
64+ assert collect_dumped_objects (result ) == ["#" , "" , "" ]
0 commit comments