-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathtest_errors.py
More file actions
258 lines (211 loc) · 6.87 KB
/
test_errors.py
File metadata and controls
258 lines (211 loc) · 6.87 KB
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
from __future__ import annotations
import textwrap
import pytest
def test_multiple_features_error(pytester):
"""Test multiple features in a single feature file."""
features = pytester.mkdir("features")
features.joinpath("test.feature").write_text(
textwrap.dedent(
"""
Feature: First Feature
Scenario: First Scenario
Given a step
Feature: Second Feature
Scenario: Second Scenario
Given another step
"""
),
encoding="utf-8",
)
pytester.makepyfile(
textwrap.dedent(
"""
from pytest_bdd import scenarios
scenarios('features')
"""
)
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(["*FeatureError: Multiple features are not allowed in a single feature file.*"])
@pytest.mark.skip(reason="Currently, pytest-bdd does not raise an error for this case but it block everything else.")
def test_step_outside_scenario_or_background_error(pytester):
"""Test step outside of a Scenario or Background."""
features = pytester.mkdir("features")
features.joinpath("test.feature").write_text(
textwrap.dedent(
"""
Feature: Invalid Feature
# Step not inside a scenario or background
Given a step that is not inside a scenario or background
Scenario: A valid scenario
Given a step inside a scenario
"""
),
encoding="utf-8",
)
pytester.makepyfile(
textwrap.dedent(
"""
from pytest_bdd import scenarios, given
@given("a step inside a scenario")
def step_inside_scenario():
pass
scenarios('features')
"""
)
)
result = pytester.runpytest()
# Expect the FeatureError for the step outside of scenario or background
result.stdout.fnmatch_lines(["*FeatureError: Step definition outside of a Scenario or a Background.*"])
def test_multiple_backgrounds_error(pytester):
"""Test multiple backgrounds in a single feature."""
features = pytester.mkdir("features")
features.joinpath("test.feature").write_text(
textwrap.dedent(
"""
Feature: Feature with multiple backgrounds
Background: First background
Given a first background step
Background: Second background
Given a second background step
Scenario: A valid scenario
Given a step in the scenario
"""
),
encoding="utf-8",
)
pytester.makepyfile(
textwrap.dedent(
"""
from pytest_bdd import scenarios
scenarios('features')
"""
)
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(
["*BackgroundError: Multiple 'Background' sections detected. Only one 'Background' is allowed per feature.*"]
)
def test_misplaced_scenario_error(pytester):
"""Test misplaced or incorrect Scenario keywords."""
features = pytester.mkdir("features")
features.joinpath("test.feature").write_text(
textwrap.dedent(
"""
Scenario: First scenario
Given a step
Scenario: Misplaced scenario
Given another step
When I have something wrong
"""
),
encoding="utf-8",
)
pytester.makepyfile(
textwrap.dedent(
"""
from pytest_bdd import scenarios, given, when
@given("a step")
def a_step():
pass
@given("another step")
def another_step():
pass
@when("I have something wrong")
def something_wrong():
pass
scenarios('features')
"""
)
)
result = pytester.runpytest()
# Expect that no ScenarioError will actually be raised here
result.stdout.fnmatch_lines(
[
"*ScenarioError: Misplaced or incorrect 'Scenario' keyword. Ensure it's correctly placed. There might be a missing Feature section.*"
]
)
def test_misplaced_rule_error(pytester):
"""Test misplaced or incorrectly formatted Rule."""
features = pytester.mkdir("features")
features.joinpath("test.feature").write_text(
textwrap.dedent(
"""
Rule: Misplaced rule
Feature: Feature with misplaced rule
Scenario: A scenario inside a rule
Given a step
"""
),
encoding="utf-8",
)
pytester.makepyfile(
textwrap.dedent(
"""
from pytest_bdd import given, scenarios
scenarios('features')
@given("a step")
def a_step():
pass
"""
)
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(
["*RuleError: Misplaced or incorrectly formatted 'Rule'. Ensure it follows the feature structure.*"]
)
def test_improper_step_error(pytester):
"""Test improper step without keyword."""
features = pytester.mkdir("features")
features.joinpath("test.feature").write_text(
textwrap.dedent(
"""
Feature: Feature with improper step
Scenario: Scenario with improper step
Given a valid step
InvalidStep I have an invalid step
"""
),
encoding="utf-8",
)
pytester.makepyfile(
textwrap.dedent(
"""
from pytest_bdd import scenarios
scenarios('features')
"""
)
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(["*TokenError: Unexpected token found. Check Gherkin syntax near the reported error.*"])
def test_improper_initial_keyword(pytester):
"""Test first step using incorrect initial keyword."""
features = pytester.mkdir("features")
features.joinpath("test.feature").write_text(
textwrap.dedent(
"""
Feature: Incorrect initial keyword
Scenario: No initial Given, When or Then
And foo
"""
),
encoding="utf-8",
)
pytester.makepyfile(
textwrap.dedent(
"""
from pytest_bdd import given, scenarios
scenarios('features')
@given("foo")
def foo():
pass
@then("bar")
def bar():
pass
"""
)
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(
["*StepError: First step in a scenario or background must start with 'Given', 'When' or 'Then', but got And.*"]
)