How to use test_all_steps_can_provide_fixtures method in Pytest-bdd

Best Python code snippet using pytest-bdd

Run Pytest-bdd automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

test_steps.py

Source: test_steps.py Github

copy
1import textwrap
2
3
4def test_steps(testdir):
5    testdir.makefile(
6        ".feature",
7        steps=textwrap.dedent(
8            """\
9            Feature: Steps are executed one by one
10                Steps are executed one by one. Given and When sections
11                are not mandatory in some cases.
12
13                Scenario: Executed step by step
14                    Given I have a foo fixture with value "foo"
15                    And there is a list
16                    When I append 1 to the list
17                    And I append 2 to the list
18                    And I append 3 to the list
19                    Then foo should have value "foo"
20                    But the list should be [1, 2, 3]
21            """
22        ),
23    )
24
25    testdir.makepyfile(
26        textwrap.dedent(
27            """\
28        from pytest_bdd import given, when, then, scenario
29
30        @scenario("steps.feature", "Executed step by step")
31        def test_steps():
32            pass
33
34        @given('I have a foo fixture with value "foo"', target_fixture="foo")
35        def foo():
36            return "foo"
37
38
39        @given("there is a list", target_fixture="results")
40        def results():
41            return []
42
43
44        @when("I append 1 to the list")
45        def append_1(results):
46            results.append(1)
47
48
49        @when("I append 2 to the list")
50        def append_2(results):
51            results.append(2)
52
53
54        @when("I append 3 to the list")
55        def append_3(results):
56            results.append(3)
57
58
59        @then('foo should have value "foo"')
60        def foo_is_foo(foo):
61            assert foo == "foo"
62
63
64        @then("the list should be [1, 2, 3]")
65        def check_results(results):
66            assert results == [1, 2, 3]
67
68        """
69        )
70    )
71    result = testdir.runpytest()
72    result.assert_outcomes(passed=1, failed=0)
73
74
75def test_all_steps_can_provide_fixtures(testdir):
76    """Test that given/when/then can all provide fixtures."""
77    testdir.makefile(
78        ".feature",
79        steps=textwrap.dedent(
80            """\
81            Feature: Step fixture
82                Scenario: Given steps can provide fixture
83                    Given Foo is "bar"
84                    Then foo should be "bar"
85                Scenario: When steps can provide fixture
86                    When Foo is "baz"
87                    Then foo should be "baz"
88                Scenario: Then steps can provide fixture
89                    Then foo is "qux"
90                    And foo should be "qux"
91            """
92        ),
93    )
94
95    testdir.makepyfile(
96        textwrap.dedent(
97            """\
98        from pytest_bdd import given, when, then, parsers, scenarios
99
100        scenarios("steps.feature")
101
102        @given(parsers.parse('Foo is "{value}"'), target_fixture="foo")
103        def given_foo_is_value(value):
104            return value
105
106
107        @when(parsers.parse('Foo is "{value}"'), target_fixture="foo")
108        def when_foo_is_value(value):
109            return value
110
111
112        @then(parsers.parse('Foo is "{value}"'), target_fixture="foo")
113        def then_foo_is_value(value):
114            return value
115
116
117        @then(parsers.parse('foo should be "{value}"'))
118        def foo_is_foo(foo, value):
119            assert foo == value
120
121        """
122        )
123    )
124    result = testdir.runpytest()
125    result.assert_outcomes(passed=3, failed=0)
126
127
128def test_when_first(testdir):
129    testdir.makefile(
130        ".feature",
131        steps=textwrap.dedent(
132            """\
133            Feature: Steps are executed one by one
134                Steps are executed one by one. Given and When sections
135                are not mandatory in some cases.
136
137                Scenario: When step can be the first
138                    When I do nothing
139                    Then I make no mistakes
140            """
141        ),
142    )
143    testdir.makepyfile(
144        textwrap.dedent(
145            """\
146        from pytest_bdd import when, then, scenario
147
148        @scenario("steps.feature", "When step can be the first")
149        def test_steps():
150            pass
151
152        @when("I do nothing")
153        def do_nothing():
154            pass
155
156
157        @then("I make no mistakes")
158        def no_errors():
159            assert True
160
161        """
162        )
163    )
164    result = testdir.runpytest()
165    result.assert_outcomes(passed=1, failed=0)
166
167
168def test_then_after_given(testdir):
169    testdir.makefile(
170        ".feature",
171        steps=textwrap.dedent(
172            """\
173            Feature: Steps are executed one by one
174                Steps are executed one by one. Given and When sections
175                are not mandatory in some cases.
176
177                Scenario: Then step can follow Given step
178                    Given I have a foo fixture with value "foo"
179                    Then foo should have value "foo"
180
181            """
182        ),
183    )
184    testdir.makepyfile(
185        textwrap.dedent(
186            """\
187        from pytest_bdd import given, then, scenario
188
189        @scenario("steps.feature", "Then step can follow Given step")
190        def test_steps():
191            pass
192
193        @given('I have a foo fixture with value "foo"', target_fixture="foo")
194        def foo():
195            return "foo"
196
197        @then('foo should have value "foo"')
198        def foo_is_foo(foo):
199            assert foo == "foo"
200
201        """
202        )
203    )
204    result = testdir.runpytest()
205    result.assert_outcomes(passed=1, failed=0)
206
207
208def test_conftest(testdir):
209    testdir.makefile(
210        ".feature",
211        steps=textwrap.dedent(
212            """\
213            Feature: Steps are executed one by one
214                Steps are executed one by one. Given and When sections
215                are not mandatory in some cases.
216
217                Scenario: All steps are declared in the conftest
218                    Given I have a bar
219                    Then bar should have value "bar"
220
221            """
222        ),
223    )
224    testdir.makeconftest(
225        textwrap.dedent(
226            """\
227        from pytest_bdd import given, then
228
229
230        @given("I have a bar", target_fixture="bar")
231        def bar():
232            return "bar"
233
234
235        @then('bar should have value "bar"')
236        def bar_is_bar(bar):
237            assert bar == "bar"
238
239        """
240        )
241    )
242    testdir.makepyfile(
243        textwrap.dedent(
244            """\
245        from pytest_bdd import scenario
246
247        @scenario("steps.feature", "All steps are declared in the conftest")
248        def test_steps():
249            pass
250
251        """
252        )
253    )
254    result = testdir.runpytest()
255    result.assert_outcomes(passed=1, failed=0)
256
257
258def test_multiple_given(testdir):
259    """Using the same given fixture raises an error."""
260    testdir.makefile(
261        ".feature",
262        steps=textwrap.dedent(
263            """\
264            Feature: Steps are executed one by one
265                Scenario: Using the same given twice
266                    Given foo is "foo"
267                    And foo is "bar"
268                    Then foo should be "bar"
269
270            """
271        ),
272    )
273    testdir.makepyfile(
274        textwrap.dedent(
275            """\
276        from pytest_bdd import parsers, given, then, scenario
277
278
279        @given(parsers.parse("foo is {value}"), target_fixture="foo")
280        def foo(value):
281            return value
282
283
284        @then(parsers.parse("foo should be {value}"))
285        def foo_should_be(foo, value):
286            assert foo == value
287
288
289        @scenario("steps.feature", "Using the same given twice")
290        def test_given_twice():
291            pass
292
293        """
294        )
295    )
296    result = testdir.runpytest()
297    result.assert_outcomes(passed=1, failed=0)
298
299
300def test_step_hooks(testdir):
301    """When step fails."""
302    testdir.makefile(
303        ".feature",
304        test="""
305    Scenario: When step has hook on failure
306        Given I have a bar
307        When it fails
308
309    Scenario: When step's dependency a has failure
310        Given I have a bar
311        When it's dependency fails
312
313    Scenario: When step is not found
314        Given not found
315
316    Scenario: When step validation error happens
317        Given foo
318        And foo
319    """,
320    )
321    testdir.makepyfile(
322        """
323        import pytest
324        from pytest_bdd import given, when, scenario
325
326        @given('I have a bar')
327        def i_have_bar():
328            return 'bar'
329
330        @when('it fails')
331        def when_it_fails():
332            raise Exception('when fails')
333
334        @given('I have a bar')
335        def i_have_bar():
336            return 'bar'
337
338        @pytest.fixture
339        def dependency():
340            raise Exception('dependency fails')
341
342        @when("it's dependency fails")
343        def when_dependency_fails(dependency):
344            pass
345
346        @scenario('test.feature', "When step's dependency a has failure")
347        def test_when_dependency_fails():
348            pass
349
350        @scenario('test.feature', 'When step has hook on failure')
351        def test_when_fails():
352            pass
353
354        @scenario('test.feature', 'When step is not found')
355        def test_when_not_found():
356            pass
357
358        @when('foo')
359        def foo():
360            return 'foo'
361
362        @scenario('test.feature', 'When step validation error happens')
363        def test_when_step_validation_error():
364            pass
365    """
366    )
367    reprec = testdir.inline_run("-k test_when_fails")
368    reprec.assertoutcome(failed=1)
369
370    calls = reprec.getcalls("pytest_bdd_before_scenario")
371    assert calls[0].request
372
373    calls = reprec.getcalls("pytest_bdd_after_scenario")
374    assert calls[0].request
375
376    calls = reprec.getcalls("pytest_bdd_before_step")
377    assert calls[0].request
378
379    calls = reprec.getcalls("pytest_bdd_before_step_call")
380    assert calls[0].request
381
382    calls = reprec.getcalls("pytest_bdd_after_step")
383    assert calls[0].request
384
385    calls = reprec.getcalls("pytest_bdd_step_error")
386    assert calls[0].request
387
388    reprec = testdir.inline_run("-k test_when_not_found")
389    reprec.assertoutcome(failed=1)
390
391    calls = reprec.getcalls("pytest_bdd_step_func_lookup_error")
392    assert calls[0].request
393
394    reprec = testdir.inline_run("-k test_when_step_validation_error")
395    reprec.assertoutcome(failed=1)
396
397    reprec = testdir.inline_run("-k test_when_dependency_fails", "-vv")
398    reprec.assertoutcome(failed=1)
399
400    calls = reprec.getcalls("pytest_bdd_before_step")
401    assert len(calls) == 2
402
403    calls = reprec.getcalls("pytest_bdd_before_step_call")
404    assert len(calls) == 1
405
406    calls = reprec.getcalls("pytest_bdd_step_error")
407    assert calls[0].request
408
409
410def test_step_trace(testdir):
411    """Test step trace."""
412    testdir.makeini(
413        """
414        [pytest]
415        console_output_style=classic
416    """
417    )
418
419    testdir.makefile(
420        ".feature",
421        test="""
422    Scenario: When step has failure
423        Given I have a bar
424        When it fails
425
426    Scenario: When step is not found
427        Given not found
428
429    Scenario: When step validation error happens
430        Given foo
431        And foo
432    """,
433    )
434    testdir.makepyfile(
435        """
436        import pytest
437        from pytest_bdd import given, when, scenario
438
439        @given('I have a bar')
440        def i_have_bar():
441            return 'bar'
442
443        @when('it fails')
444        def when_it_fails():
445            raise Exception('when fails')
446
447        @scenario('test.feature', 'When step has failure')
448        def test_when_fails_inline():
449            pass
450
451        @scenario('test.feature', 'When step has failure')
452        def test_when_fails_decorated():
453            pass
454
455        @scenario('test.feature', 'When step is not found')
456        def test_when_not_found():
457            pass
458
459        @when('foo')
460        def foo():
461            return 'foo'
462
463        @scenario('test.feature', 'When step validation error happens')
464        def test_when_step_validation_error():
465            pass
466    """
467    )
468    result = testdir.runpytest("-k test_when_fails_inline", "-vv")
469    result.assert_outcomes(failed=1)
470    result.stdout.fnmatch_lines(["*test_when_fails_inline*FAILED"])
471    assert "INTERNALERROR" not in result.stdout.str()
472
473    result = testdir.runpytest("-k test_when_fails_decorated", "-vv")
474    result.assert_outcomes(failed=1)
475    result.stdout.fnmatch_lines(["*test_when_fails_decorated*FAILED"])
476    assert "INTERNALERROR" not in result.stdout.str()
477
478    result = testdir.runpytest("-k test_when_not_found", "-vv")
479    result.assert_outcomes(failed=1)
480    result.stdout.fnmatch_lines(["*test_when_not_found*FAILED"])
481    assert "INTERNALERROR" not in result.stdout.str()
482
483    result = testdir.runpytest("-k test_when_step_validation_error", "-vv")
484    result.assert_outcomes(failed=1)
485    result.stdout.fnmatch_lines(["*test_when_step_validation_error*FAILED"])
486    assert "INTERNALERROR" not in result.stdout.str()
487
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Pytest-bdd on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)