Skip to content

Commit dd3266d

Browse files
committed
Add test to make sure that converter for parser for empty strings in example tables works as expected
Resolves #551
1 parent 93e446e commit dd3266d

File tree

1 file changed

+87
-25
lines changed

1 file changed

+87
-25
lines changed

tests/feature/test_outline_empty_values.py

Lines changed: 87 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,6 @@
44

55
from 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
10-
11-
# Using `parsers.re` so that we can match empty values
12-
13-
@given(parsers.re("there are (?P<start>.*?) cucumbers"))
14-
def _(start):
15-
dump_obj(start)
16-
17-
18-
@when(parsers.re("I eat (?P<eat>.*?) cucumbers"))
19-
def _(eat):
20-
dump_obj(eat)
21-
22-
23-
@then(parsers.re("I should have (?P<left>.*?) cucumbers"))
24-
def _(left):
25-
dump_obj(left)
26-
27-
"""
28-
297

308
def test_scenario_with_empty_example_values(pytester):
319
pytester.makefile(
@@ -44,14 +22,36 @@ def test_scenario_with_empty_example_values(pytester):
4422
"""
4523
),
4624
)
47-
pytester.makeconftest(textwrap.dedent(STEPS))
25+
pytester.makeconftest(
26+
textwrap.dedent(
27+
"""\
28+
from pytest_bdd import given, when, then, parsers
29+
from pytest_bdd.utils import dump_obj
30+
31+
# Using `parsers.re` so that we can match empty values
32+
33+
@given(parsers.re("there are (?P<start>.*?) cucumbers"))
34+
def _(start):
35+
dump_obj(start)
36+
37+
38+
@when(parsers.re("I eat (?P<eat>.*?) cucumbers"))
39+
def _(eat):
40+
dump_obj(eat)
41+
42+
43+
@then(parsers.re("I should have (?P<left>.*?) cucumbers"))
44+
def _(left):
45+
dump_obj(left)
46+
47+
"""
48+
)
49+
)
4850

4951
pytester.makepyfile(
5052
textwrap.dedent(
5153
"""\
52-
from pytest_bdd.utils import dump_obj
5354
from pytest_bdd import scenario
54-
import json
5555
5656
@scenario("outline.feature", "Outlined with empty example values")
5757
def test_outline():
@@ -62,3 +62,65 @@ def test_outline():
6262
result = pytester.runpytest("-s")
6363
result.assert_outcomes(passed=1)
6464
assert collect_dumped_objects(result) == ["#", "", ""]
65+
66+
67+
def test_scenario_with_empty_example_values_none_transformer(pytester):
68+
pytester.makefile(
69+
".feature",
70+
outline=textwrap.dedent(
71+
"""\
72+
Feature: Outline
73+
Scenario Outline: Outlined with empty example values and transformer
74+
Given there are <start> cucumbers
75+
When I eat <eat> cucumbers
76+
Then I should have <left> cucumbers
77+
78+
Examples:
79+
| start | eat | left |
80+
| # | | |
81+
"""
82+
),
83+
)
84+
pytester.makeconftest(
85+
textwrap.dedent(
86+
"""\
87+
from pytest_bdd import given, when, then, parsers
88+
from pytest_bdd.utils import dump_obj
89+
90+
91+
def empty_to_none(value):
92+
return None if value.strip() == "" else value
93+
94+
95+
@given(parsers.re("there are (?P<start>.*?) cucumbers"), converters={"start": empty_to_none})
96+
def _(start):
97+
dump_obj(start)
98+
99+
100+
@when(parsers.re("I eat (?P<eat>.*?) cucumbers"), converters={"eat": empty_to_none})
101+
def _(eat):
102+
dump_obj(eat)
103+
104+
105+
@then(parsers.re("I should have (?P<left>.*?) cucumbers"), converters={"left": empty_to_none})
106+
def _(left):
107+
dump_obj(left)
108+
109+
"""
110+
)
111+
)
112+
113+
pytester.makepyfile(
114+
textwrap.dedent(
115+
"""\
116+
from pytest_bdd import scenario
117+
118+
@scenario("outline.feature", "Outlined with empty example values and transformer")
119+
def test_outline():
120+
pass
121+
"""
122+
)
123+
)
124+
result = pytester.runpytest("-s")
125+
result.assert_outcomes(passed=1)
126+
assert collect_dumped_objects(result) == ["#", None, None]

0 commit comments

Comments
 (0)