Debian Codegraph @

features/fixture.feature

raw
1 Feature: Fixture
2
3 As a BDD writer
4 I want to be able to use fixtures (scope guard)
5 So that it is easy to provide setup/cleanup functionality.
6
7 . SPECIFICATION:
8 . A fixture provides common test-support functionality.
9 . It contains the setup functionality and in many cases a teardown/cleanup part.
10 . To simplify writing fixtures, setup and cleanup part are in the same function
11 . (same as: @contextlib.contextmanager, @pytest.fixture).
12 .
13 . In most case, you want to use a fixture-tag in a "*.feature" file (in Gherkin)
14 . to mark that a scenario or feature should use a specific fixture.
15 .
16 . EXAMPLE:
17 .
18 . # -- FILE: features/environment.py
19 . @fixture
20 . def fixture_foo(context, *args, **kwargs): # -- CASE: generator-function
21 . the_fixture = setup_fixture_foo(*args, **kwargs)
22 . context.foo = the_fixture
23 . yield the_fixture
24 . cleanup_fixture_foo(the_fixture) # -- SKIPPED: On fixture-setup-error
25 .
26 . @fixture(name="fixture.bar")
27 . def fixture_bar(context, *args, **kwargs): # -- CASE: function
28 . the_fixture = setup_fixture_bar(*args, **kwargs)
29 . context.bar = the_fixture
30 . context.add_cleanup(the_fixture.cleanup)
31 . return the_fixture
32 .
33 . # -- ENVIRONMENT-HOOKS:
34 . def before_tag(context, tag):
35 . if tag == "fixture.foo":
36 . # -- NOTE: Calls setup_fixture part (until yield statement) and
37 . # registers cleanup_fixture which will be called when
38 . # context scope is left (after scenario, feature or test-run).
39 . the_fixture = use_fixture(fixture_foo, context)
40
41 @setup
42 Scenario: Feature Setup
43 Given a new working directory
44 And a file named "features/steps/steps.py" with:
45 """
46 from behave import step
47
48 @step(u'the browser is "{browser_name}"')
49 def step_browser_is(context, browser_name):
50 assert context.browser == browser_name
51
52 @step(u'no browser info exists')
53 def step_no_browser_info(context):
54 assert not hasattr(context, "browser")
55
56 @step(u'{word:w} step passes')
57 def step_passes(context, word):
58 pass
59 """
60 And a file named "behave.ini" with:
61 """
62 [behave]
63 show_timings = false
64 """
65 And an empty file named "features/environment.py"
66
67
68 Scenario: Use fixture with generator-function (setup/cleanup)
69 Given a file named "features/environment.py" with:
70 """
71 from __future__ import print_function
72 from behave.fixture import fixture, use_fixture
73
74 @fixture(name="browser.firefox")
75 def browser_firefox(context):
76 print("FIXTURE-SETUP: browser.firefox")
77 context.browser = "firefox"
78 yield
79 print("FIXTURE-CLEANUP: browser.firefox")
80 # -- SCOPE-CLEANUP OR EXPLICIT: del context.browser
81
82 def before_tag(context, tag):
83 if tag == "fixture.browser.firefox":
84 use_fixture(browser_firefox, context)
85 """
86 And a file named "features/alice.feature" with:
87 """
88 Feature: Fixture setup/teardown
89 @fixture.browser.firefox
90 Scenario: Fixture with browser=firefox
91 Given the browser is "firefox"
92
93 Scenario: Fixture Cleanup check
94 Then no browser info exists
95 """
96 When I run "behave -f plain features/alice.feature"
97 Then it should pass with:
98 """
99 2 scenarios passed, 0 failed, 0 skipped
100 2 steps passed, 0 failed, 0 skipped, 0 undefined
101 """
102 And the command output should contain:
103 """
104 FIXTURE-SETUP: browser.firefox
105 Scenario: Fixture with browser=firefox
106 Given the browser is "firefox" ... passed
107 FIXTURE-CLEANUP: browser.firefox
108 """
109
110 Scenario: Use fixture with function (setup-only)
111 Given a file named "features/environment.py" with:
112 """
113 from __future__ import print_function
114 from behave.fixture import fixture, use_fixture
115
116 @fixture(name="browser.chrome")
117 def browser_chrome(context):
118 # -- CASE: Setup-only
119 print("FIXTURE-SETUP: browser.chrome")
120 context.browser = "chrome"
121
122 def before_tag(context, tag):
123 if tag == "fixture.browser.chrome":
124 use_fixture(browser_chrome, context)
125 """
126 And a file named "features/bob.feature" with:
127 """
128 Feature: Fixture setup only
129 @fixture.browser.chrome
130 Scenario: Fixture with browser=chrome
131 Given the browser is "chrome"
132
133 Scenario: Fixture Cleanup check
134 Then no browser info exists
135 """
136 When I run "behave -f plain features/bob.feature"
137 Then it should pass with:
138 """
139 2 scenarios passed, 0 failed, 0 skipped
140 2 steps passed, 0 failed, 0 skipped, 0 undefined
141 """
142 And the command output should contain:
143 """
144 FIXTURE-SETUP: browser.chrome
145 Scenario: Fixture with browser=chrome
146 Given the browser is "chrome" ... passed
147
148 Scenario: Fixture Cleanup check
149 Then no browser info exists ... passed
150 """
151
152 Scenario: Use fixture (case: feature)
153 Given a file named "features/environment.py" with:
154 """
155 from __future__ import print_function
156 from behave.fixture import fixture, use_fixture
157
158 @fixture
159 def foo(context):
160 print("FIXTURE-SETUP: foo")
161 yield
162 print("FIXTURE-CLEANUP: foo")
163
164 def before_tag(context, tag):
165 if tag == "fixture.foo":
166 use_fixture(foo, context)
167
168 def after_feature(context, feature):
169 print("HOOK-CALLED: after_feature: %s" % feature.name)
170 """
171 And a file named "features/use2.feature" with:
172 """
173 @fixture.foo
174 Feature: Use Fixture for Feature
175
176 Scenario:
177 Given a step passes
178
179 Scenario:
180 Then another step passes
181 """
182 When I run "behave -f plain features/use2.feature"
183 Then it should pass with:
184 """
185 1 feature passed, 0 failed, 0 skipped
186 2 scenarios passed, 0 failed, 0 skipped
187 2 steps passed, 0 failed, 0 skipped, 0 undefined
188 """
189 And the command output should contain:
190 """
191 FIXTURE-SETUP: foo
192 Feature: Use Fixture for Feature
193 Scenario:
194 Given a step passes ... passed
195
196 Scenario:
197 Then another step passes ... passed
198
199 HOOK-CALLED: after_feature: Use Fixture for Feature
200 FIXTURE-CLEANUP: foo
201 """
202 But note that "the fixture-cleanup after the feature"
203
204
205 Scenario: Use fixture (case: step)
206 Given a file named "features/environment.py" with:
207 """
208 from __future__ import print_function
209
210 def after_scenario(context, scenario):
211 print("HOOK-CALLED: after_scenario: %s" % scenario.name)
212 """
213 And an empty file named "behave4me/__init__.py"
214 And a file named "behave4me/fixtures.py" with:
215 """
216 from __future__ import print_function
217 from behave import fixture
218
219 @fixture
220 def foo(context, name):
221 print("FIXTURE-SETUP: foo%s" % name)
222 yield 42
223 print("FIXTURE-CLEANUP: foo%s" % name)
224 """
225 And a file named "features/steps/fixture_steps2.py" with:
226 """
227 from __future__ import print_function
228 from behave import step, use_fixture
229 from behave4me.fixtures import foo
230
231 @step(u'I use fixture "{fixture_name}"')
232 def step_use_fixture(context, fixture_name):
233 if fixture_name.startswith("foo"):
234 name = fixture_name.replace("foo", "")
235 the_fixture = use_fixture(foo, context, name)
236 setattr(context, fixture_name, the_fixture)
237 else:
238 raise LookupError("Unknown fixture: %s" % fixture_name)
239 """
240 And a file named "features/use3.feature" with:
241 """
242 @fixture.foo
243 Feature:
244
245 Scenario: Use Fixture
246 Given I use fixture "foo_1"
247 Then a step passes
248
249 Scenario:
250 Then another step passes
251 """
252 When I run "behave -f plain features/use3.feature"
253 Then it should pass with:
254 """
255 2 scenarios passed, 0 failed, 0 skipped
256 3 steps passed, 0 failed, 0 skipped, 0 undefined
257 """
258 And the command output should contain:
259 """
260 Feature:
261 Scenario: Use Fixture
262 Given I use fixture "foo_1" ... passed
263 Then a step passes ... passed
264 HOOK-CALLED: after_scenario: Use Fixture
265 FIXTURE-CLEANUP: foo_1
266 """
267 But note that "the fixture-cleanup occurs after the scenario"
268
269
270 Scenario: Use multiple fixtures (with setup/cleanup)
271 Given a file named "features/environment.py" with:
272 """
273 from __future__ import print_function
274 from behave.fixture import fixture, use_fixture
275
276 @fixture
277 def foo(context):
278 print("FIXTURE-SETUP: foo")
279 yield
280 print("FIXTURE-CLEANUP: foo")
281
282 @fixture
283 def bar(context):
284 def cleanup_bar():
285 print("FIXTURE-CLEANUP: bar")
286
287 print("FIXTURE-SETUP: bar")
288 context.add_cleanup(cleanup_bar)
289
290 def before_tag(context, tag):
291 if tag == "fixture.foo":
292 use_fixture(foo, context)
293 elif tag == "fixture.bar":
294 use_fixture(bar, context)
295 """
296 And a file named "features/two.feature" with:
297 """
298 Feature: Use multiple Fixtures
299 @fixture.foo
300 @fixture.bar
301 Scenario: Two Fixtures
302 Given a step passes
303
304 Scenario:
305 Then another step passes
306 """
307 When I run "behave -f plain features/two.feature"
308 Then it should pass with:
309 """
310 2 scenarios passed, 0 failed, 0 skipped
311 2 steps passed, 0 failed, 0 skipped, 0 undefined
312 """
313 And the command output should contain:
314 """
315 FIXTURE-SETUP: foo
316 FIXTURE-SETUP: bar
317 Scenario: Two Fixtures
318 Given a step passes ... passed
319 FIXTURE-CLEANUP: bar
320 FIXTURE-CLEANUP: foo
321 """
322 But note that "the fixture-cleanup occurs in reverse order (LIFO)"
323
324
325 Scenario: Use same fixture twice with different args
326 Given a file named "features/environment.py" with:
327 """
328 from __future__ import print_function
329 from behave.fixture import fixture, use_fixture
330
331 @fixture
332 def foo(context, name):
333 name2 = "foo%s" % name
334 print("FIXTURE-SETUP: %s" % name2)
335 setattr(context, name2, 1)
336 yield
337 print("FIXTURE-CLEANUP: %s" % name2)
338
339 def before_tag(context, tag):
340 if tag.startswith("fixture.foo"):
341 # -- FIXTURE-TAG SCHEMA: fixture.foo*
342 name = tag.replace("fixture.foo", "")
343 use_fixture(foo, context, name)
344 """
345 And a file named "features/two.feature" with:
346 """
347 Feature: Use same Fixture twice
348 @fixture.foo_1
349 @fixture.foo_2
350 Scenario: Use Fixtures
351 Given a step passes
352
353 Scenario:
354 Then another step passes
355 """
356 When I run "behave -f plain features/two.feature"
357 Then it should pass with:
358 """
359 2 scenarios passed, 0 failed, 0 skipped
360 2 steps passed, 0 failed, 0 skipped, 0 undefined
361 """
362 And the command output should contain:
363 """
364 FIXTURE-SETUP: foo_1
365 FIXTURE-SETUP: foo_2
366 Scenario: Use Fixtures
367 Given a step passes ... passed
368 FIXTURE-CLEANUP: foo_2
369 FIXTURE-CLEANUP: foo_1
370 """
371 But note that "the fixture-cleanup occurs in reverse order (LIFO)"
372
373
374 Scenario: Use invalid fixture (with two yields or more)
375 Given a file named "features/environment.py" with:
376 """
377 from __future__ import print_function
378 from behave.fixture import fixture, use_fixture
379
380 @fixture
381 def invalid_fixture(context):
382 # -- CASE: Setup-only
383 print("FIXTURE-SETUP: invalid")
384 yield
385 print("FIXTURE-CLEANUP: invalid")
386 yield
387 print("OOPS: Too many yields used")
388
389 def before_tag(context, tag):
390 if tag == "fixture.invalid":
391 use_fixture(invalid_fixture, context)
392 """
393 And a file named "features/invalid_fixture.feature" with:
394 """
395 Feature: Fixture with more than one yield
396 @fixture.invalid
397 Scenario: Use invalid fixture
398 Given a step passes
399
400 Scenario:
401 Then another step passes
402 """
403 When I run "behave -f plain features/invalid_fixture.feature"
404 Then it should fail with:
405 """
406 1 scenario passed, 1 failed, 0 skipped
407 2 steps passed, 0 failed, 0 skipped, 0 undefined
408 """
409 And the command output should contain:
410 """
411 FIXTURE-SETUP: invalid
412 Scenario: Use invalid fixture
413 Given a step passes ... passed
414 FIXTURE-CLEANUP: invalid
415 CLEANUP-ERROR in cleanup_fixture: InvalidFixtureError: Has more than one yield: <function invalid_fixture at
416 """
417 But note that "cleanup errors cause scenario to fail (by default)"
418
419
420 Scenario: Fixture with cleanup-error causes failed (case: scenario)
421 Given a file named "features/environment.py" with:
422 """
423 from __future__ import print_function
424 from behave.fixture import fixture, use_fixture
425
426 @fixture
427 def bad_with_cleanup_error(context):
428 print("FIXTURE-SETUP: bad_with_cleanup_error")
429 yield
430 print("FIXTURE-CLEANUP: bad_with_cleanup_error")
431 raise RuntimeError("BAD_FIXTURE_CLEANUP_ERROR")
432
433 def before_tag(context, tag):
434 if tag == "fixture.bad_with_cleanup_error":
435 use_fixture(bad_with_cleanup_error, context)
436 """
437 And a file named "features/bad_fixture.feature" with:
438 """
439 Feature: Fixture with cleanup error
440 @fixture.bad_with_cleanup_error
441 Scenario: Use fixture
442 Given a step passes
443
444 Scenario:
445 Then another step passes
446 """
447 When I run "behave -f plain features/bad_fixture.feature"
448 Then it should fail with:
449 """
450 1 scenario passed, 1 failed, 0 skipped
451 2 steps passed, 0 failed, 0 skipped, 0 undefined
452 """
453 And the command output should contain:
454 """
455 FIXTURE-SETUP: bad_with_cleanup_error
456 Scenario: Use fixture
457 Given a step passes ... passed
458 FIXTURE-CLEANUP: bad_with_cleanup_error
459 CLEANUP-ERROR in cleanup_fixture: RuntimeError: BAD_FIXTURE_CLEANUP_ERROR
460 """
461 And the command output should contain:
462 """
463 File "features/environment.py", line 9, in bad_with_cleanup_error
464 raise RuntimeError("BAD_FIXTURE_CLEANUP_ERROR")
465 RuntimeError: BAD_FIXTURE_CLEANUP_ERROR
466 """
467 But note that "traceback shows cleanup-fixture failure location"
468 And note that "cleanup error causes scenario to fail (by default)"
469
470
471 Scenario: Multiple fixture cleanup-errors cause no abort after first error (case: scenario)
472 Given a file named "features/environment.py" with:
473 """
474 from __future__ import print_function
475 from behave.fixture import fixture, use_fixture
476
477 @fixture
478 def foo(context):
479 print("FIXTURE-SETUP: foo")
480 yield
481 print("FIXTURE-CLEANUP: foo")
482
483 @fixture
484 def bad_with_cleanup_error(context, name):
485 print("FIXTURE-SETUP: bad_with_cleanup_error%s" % name)
486 yield
487 print("FIXTURE-CLEANUP: bad_with_cleanup_error%s" % name)
488 raise RuntimeError("BAD_FIXTURE_CLEANUP_ERROR%s" % name)
489
490 def before_tag(context, tag):
491 if tag.startswith("fixture.bad_with_cleanup_error"):
492 name = tag.replace("fixture.bad_with_cleanup_error", "")
493 use_fixture(bad_with_cleanup_error, context, name)
494 elif tag.startswith("fixture.foo"):
495 use_fixture(foo, context)
496 """
497 And a file named "features/bad_fixture2.feature" with:
498 """
499 Feature: Fixture with cleanup error
500 @fixture.bad_with_cleanup_error_1
501 @fixture.foo
502 @fixture.bad_with_cleanup_error_2
503 Scenario: Use fixture
504 Given a step passes
505
506 Scenario:
507 Then another step passes
508 """
509 When I run "behave -f plain features/bad_fixture2.feature"
510 Then it should fail with:
511 """
512 1 scenario passed, 1 failed, 0 skipped
513 2 steps passed, 0 failed, 0 skipped, 0 undefined
514 """
515 And the command output should contain:
516 """
517 FIXTURE-SETUP: bad_with_cleanup_error_1
518 FIXTURE-SETUP: foo
519 FIXTURE-SETUP: bad_with_cleanup_error_2
520 Scenario: Use fixture
521 Given a step passes ... passed
522 FIXTURE-CLEANUP: bad_with_cleanup_error_2
523 CLEANUP-ERROR in cleanup_fixture: RuntimeError: BAD_FIXTURE_CLEANUP_ERROR_2
524 """
525 And the command output should contain:
526 """
527 FIXTURE-CLEANUP: foo
528 FIXTURE-CLEANUP: bad_with_cleanup_error_1
529 CLEANUP-ERROR in cleanup_fixture: RuntimeError: BAD_FIXTURE_CLEANUP_ERROR_1
530 """
531 But note that "all fixture-cleanups are executed (even when errors occur)"
532 And note that "fixture-cleanups are executed in reverse order (LIFO)"