1
1
.. _yieldfixture :
2
2
3
- Fixture functions using "yield" / context manager integration
3
+ "yield_fixture" functions
4
4
---------------------------------------------------------------
5
5
6
6
.. deprecated :: 2.10
@@ -10,98 +10,8 @@ Fixture functions using "yield" / context manager integration
10
10
.. important ::
11
11
Since pytest-2.10, fixtures using the normal ``fixture `` decorator can use a ``yield ``
12
12
statement to provide fixture values and execute teardown code, exactly like ``yield_fixture ``
13
- described in this session .
13
+ in previous versions .
14
14
15
- .. regendoc:wipe
15
+ Marking functions as ``yield_fixture `` is still supported, but deprecated and should not
16
+ be used in new code.
16
17
17
- pytest-2.4 allows fixture functions to seamlessly use a ``yield `` instead
18
- of a ``return `` statement to provide a fixture value while otherwise
19
- fully supporting all other fixture features.
20
-
21
- Let's look at a simple standalone-example using the ``yield `` syntax::
22
-
23
- # content of test_yield.py
24
-
25
- import pytest
26
-
27
- @pytest.yield_fixture
28
- def passwd():
29
- print ("\nsetup before yield")
30
- f = open("/etc/passwd")
31
- yield f.readlines()
32
- print ("teardown after yield")
33
- f.close()
34
-
35
- def test_has_lines(passwd):
36
- print ("test called")
37
- assert passwd
38
-
39
- In contrast to :ref: `finalization through registering callbacks
40
- <finalization>`, our fixture function used a ``yield ``
41
- statement to provide the lines of the ``/etc/passwd `` file.
42
- The code after the ``yield `` statement serves as the teardown code,
43
- avoiding the indirection of registering a teardown callback function.
44
-
45
- Let's run it with output capturing disabled::
46
-
47
- $ py.test -q -s test_yield.py
48
-
49
- setup before yield
50
- test called
51
- .teardown after yield
52
-
53
- 1 passed in 0.12 seconds
54
-
55
- We can also seamlessly use the new syntax with ``with `` statements.
56
- Let's simplify the above ``passwd `` fixture::
57
-
58
- # content of test_yield2.py
59
-
60
- import pytest
61
-
62
- @pytest.yield_fixture
63
- def passwd():
64
- with open("/etc/passwd") as f:
65
- yield f.readlines()
66
-
67
- def test_has_lines(passwd):
68
- assert len(passwd) >= 1
69
-
70
- The file ``f `` will be closed after the test finished execution
71
- because the Python ``file `` object supports finalization when
72
- the ``with `` statement ends.
73
-
74
- Note that the yield fixture form supports all other fixture
75
- features such as ``scope ``, ``params ``, etc., thus changing existing
76
- fixture functions to use ``yield `` is straightforward.
77
-
78
- .. note ::
79
-
80
- While the ``yield `` syntax is similar to what
81
- :py:func: `contextlib.contextmanager ` decorated functions
82
- provide, with pytest fixture functions the part after the
83
- "yield" will always be invoked, independently from the
84
- exception status of the test function which uses the fixture.
85
- This behaviour makes sense if you consider that many different
86
- test functions might use a module or session scoped fixture.
87
-
88
-
89
- Discussion and future considerations / feedback
90
- ++++++++++++++++++++++++++++++++++++++++++++++++++++
91
-
92
- There are some topics that are worth mentioning:
93
-
94
- - usually ``yield `` is used for producing multiple values.
95
- But fixture functions can only yield exactly one value.
96
- Yielding a second fixture value will get you an error.
97
- It's possible we can evolve pytest to allow for producing
98
- multiple values as an alternative to current parametrization.
99
- For now, you can just use the normal
100
- :ref: `fixture parametrization <fixture-parametrize >`
101
- mechanisms together with ``yield ``-style fixtures.
102
-
103
- - lastly ``yield `` introduces more than one way to write
104
- fixture functions, so what's the obvious way to a newcomer?
105
-
106
- If you want to feedback or participate in discussion of the above
107
- topics, please join our :ref: `contact channels `, you are most welcome.
0 commit comments