forked from pytest-dev/pytest-bdd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_datatable.py
259 lines (202 loc) · 6.74 KB
/
test_datatable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
from __future__ import annotations
import textwrap
from src.pytest_bdd.utils import collect_dumped_objects
def test_steps_with_datatables(pytester):
pytester.makefile(
".feature",
datatable=textwrap.dedent(
"""\
Feature: Manage user accounts
Scenario: Creating a new user with roles and permissions
Given the following user details:
| name | email | age |
| John | [email protected] | 30 |
| Alice | [email protected] | 25 |
When the user is assigned the following roles:
| role | description |
| Admin | Full access to the system |
| Contributor | Can add content |
And this step has no datatable
Then the user should have the following permissions:
| permission | allowed |
| view dashboard | true |
| edit content | true |
| delete content | false |
"""
),
)
pytester.makeconftest(
textwrap.dedent(
"""\
from pytest_bdd import given, when, then
from pytest_bdd.utils import dump_obj
@given("the following user details:")
def _(datatable):
given_datatable = datatable
dump_obj(given_datatable)
@when("the user is assigned the following roles:")
def _(datatable):
when_datatable = datatable
dump_obj(when_datatable)
@when("this step has no datatable")
def _():
pass
@then("the user should have the following permissions:")
def _(datatable):
then_datatable = datatable
dump_obj(then_datatable)
"""
)
)
pytester.makepyfile(
textwrap.dedent(
"""\
from pytest_bdd import scenario
@scenario("datatable.feature", "Creating a new user with roles and permissions")
def test_datatable():
pass
"""
)
)
result = pytester.runpytest("-s")
result.assert_outcomes(passed=1)
datatables = collect_dumped_objects(result)
assert datatables[0] == [
["name", "email", "age"],
["John", "[email protected]", "30"],
["Alice", "[email protected]", "25"],
]
assert datatables[1] == [
["role", "description"],
["Admin", "Full access to the system"],
["Contributor", "Can add content"],
]
assert datatables[2] == [
["permission", "allowed"],
["view dashboard", "true"],
["edit content", "true"],
["delete content", "false"],
]
def test_datatable_argument_in_step_impl_is_optional(pytester):
pytester.makefile(
".feature",
optional_arg_datatable=textwrap.dedent(
"""\
Feature: Missing data table
Scenario: Data table is missing for a step
Given this step has a data table:
| name | email | age |
| John | [email protected] | 30 |
| Alice | [email protected] | 25 |
When this step has no data table but tries to use the datatable argument
Then an error is thrown
"""
),
)
pytester.makeconftest(
textwrap.dedent(
"""\
from pytest_bdd import given, when, then
@given("this step has a data table:")
def _(datatable):
print(datatable)
@when("this step has no data table but tries to use the datatable argument")
def _(datatable):
print(datatable)
@then("an error is thrown")
def _(datatable):
pass
"""
)
)
pytester.makepyfile(
textwrap.dedent(
"""\
from pytest_bdd import scenarios
scenarios("optional_arg_datatable.feature")
"""
)
)
result = pytester.runpytest("-s")
result.assert_outcomes(failed=1)
result.stdout.fnmatch_lines(["*fixture 'datatable' not found*"])
def test_steps_with_datatable_missing_argument_in_step(pytester):
pytester.makefile(
".feature",
missing_datatable_arg=textwrap.dedent(
"""\
Feature: Missing datatable
Scenario: Datatable arg is missing for a step definition
Given this step has a datatable
| name | email | age |
| John | [email protected] | 30 |
When this step has a datatable but no datatable argument
| name | email | age |
| John | [email protected] | 30 |
Then the test passes
"""
),
)
pytester.makeconftest(
textwrap.dedent(
"""\
from pytest_bdd import given, when, then
@given("this step has a datatable")
def _(datatable):
print(datatable)
@when("this step has a datatable but no datatable argument")
def _():
pass
@then("the test passes")
def _():
pass
"""
)
)
pytester.makepyfile(
textwrap.dedent(
"""\
from pytest_bdd import scenario
@scenario("missing_datatable_arg.feature", "Datatable arg is missing for a step definition")
def test_datatable():
pass
"""
)
)
result = pytester.runpytest("-s")
result.assert_outcomes(passed=1)
def test_datatable_step_argument_is_reserved_and_cannot_be_used(pytester):
pytester.makefile(
".feature",
reserved_datatable_arg=textwrap.dedent(
"""\
Feature: Reserved datatable argument
Scenario: Reserved datatable argument
Given this step has a {datatable} argument
Then the test fails
"""
),
)
pytester.makepyfile(
textwrap.dedent(
"""\
from pytest_bdd import scenario, given, then, parsers
@scenario("reserved_datatable_arg.feature", "Reserved datatable argument")
def test_datatable():
pass
@given(parsers.parse("this step has a {datatable} argument"))
def _(datatable):
pass
@then("the test fails")
def _():
pass
"""
)
)
result = pytester.runpytest()
result.assert_outcomes(failed=1)
result.stdout.fnmatch_lines(
[
"*Step 'this step has a {datatable} argument' defines argument names that are reserved: 'datatable'. Please use different names.*"
]
)