2013-09-30 19:42:39 +08:00
|
|
|
.. _yieldfixture:
|
2013-09-27 16:21:23 +08:00
|
|
|
|
|
|
|
Fixture functions using "yield" / context manager integration
|
|
|
|
---------------------------------------------------------------
|
|
|
|
|
2016-06-08 07:59:58 +08:00
|
|
|
.. deprecated:: 2.10
|
|
|
|
|
2013-09-27 16:21:23 +08:00
|
|
|
.. versionadded:: 2.4
|
|
|
|
|
2016-06-08 07:59:58 +08:00
|
|
|
.. important::
|
|
|
|
Since pytest-2.10, fixtures using the normal ``fixture`` decorator can use a ``yield``
|
|
|
|
statement to provide fixture values and execute teardown code, exactly like ``yield_fixture``
|
|
|
|
described in this session.
|
|
|
|
|
2013-09-27 16:21:23 +08:00
|
|
|
.. regendoc:wipe
|
|
|
|
|
2013-10-01 20:30:53 +08:00
|
|
|
pytest-2.4 allows fixture functions to seamlessly use a ``yield`` instead
|
2013-09-27 16:21:23 +08:00
|
|
|
of a ``return`` statement to provide a fixture value while otherwise
|
2015-04-04 03:28:20 +08:00
|
|
|
fully supporting all other fixture features.
|
2013-09-27 16:21:23 +08:00
|
|
|
|
2015-04-04 03:55:10 +08:00
|
|
|
Let's look at a simple standalone-example using the ``yield`` syntax::
|
2013-09-27 16:21:23 +08:00
|
|
|
|
|
|
|
# content of test_yield.py
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2013-09-30 19:42:39 +08:00
|
|
|
@pytest.yield_fixture
|
2013-09-27 16:21:23 +08:00
|
|
|
def passwd():
|
|
|
|
print ("\nsetup before yield")
|
|
|
|
f = open("/etc/passwd")
|
|
|
|
yield f.readlines()
|
|
|
|
print ("teardown after yield")
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def test_has_lines(passwd):
|
|
|
|
print ("test called")
|
|
|
|
assert passwd
|
|
|
|
|
|
|
|
In contrast to :ref:`finalization through registering callbacks
|
|
|
|
<finalization>`, our fixture function used a ``yield``
|
|
|
|
statement to provide the lines of the ``/etc/passwd`` file.
|
|
|
|
The code after the ``yield`` statement serves as the teardown code,
|
|
|
|
avoiding the indirection of registering a teardown callback function.
|
|
|
|
|
|
|
|
Let's run it with output capturing disabled::
|
|
|
|
|
|
|
|
$ py.test -q -s test_yield.py
|
|
|
|
|
|
|
|
setup before yield
|
|
|
|
test called
|
|
|
|
.teardown after yield
|
|
|
|
|
2015-06-07 05:30:49 +08:00
|
|
|
1 passed in 0.12 seconds
|
2013-09-27 16:21:23 +08:00
|
|
|
|
2014-07-10 06:00:24 +08:00
|
|
|
We can also seamlessly use the new syntax with ``with`` statements.
|
2013-09-27 16:21:23 +08:00
|
|
|
Let's simplify the above ``passwd`` fixture::
|
|
|
|
|
|
|
|
# content of test_yield2.py
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2013-09-30 19:42:39 +08:00
|
|
|
@pytest.yield_fixture
|
2013-09-27 16:21:23 +08:00
|
|
|
def passwd():
|
|
|
|
with open("/etc/passwd") as f:
|
|
|
|
yield f.readlines()
|
|
|
|
|
|
|
|
def test_has_lines(passwd):
|
|
|
|
assert len(passwd) >= 1
|
|
|
|
|
|
|
|
The file ``f`` will be closed after the test finished execution
|
|
|
|
because the Python ``file`` object supports finalization when
|
|
|
|
the ``with`` statement ends.
|
|
|
|
|
2015-04-04 03:55:10 +08:00
|
|
|
Note that the yield fixture form supports all other fixture
|
|
|
|
features such as ``scope``, ``params``, etc., thus changing existing
|
2015-04-04 04:06:51 +08:00
|
|
|
fixture functions to use ``yield`` is straightforward.
|
2013-09-27 16:21:23 +08:00
|
|
|
|
2015-04-04 03:59:33 +08:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
While the ``yield`` syntax is similar to what
|
|
|
|
:py:func:`contextlib.contextmanager` decorated functions
|
|
|
|
provide, with pytest fixture functions the part after the
|
|
|
|
"yield" will always be invoked, independently from the
|
|
|
|
exception status of the test function which uses the fixture.
|
|
|
|
This behaviour makes sense if you consider that many different
|
|
|
|
test functions might use a module or session scoped fixture.
|
|
|
|
|
2015-04-04 06:44:06 +08:00
|
|
|
|
|
|
|
Discussion and future considerations / feedback
|
|
|
|
++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
|
|
|
|
There are some topics that are worth mentioning:
|
|
|
|
|
|
|
|
- usually ``yield`` is used for producing multiple values.
|
|
|
|
But fixture functions can only yield exactly one value.
|
|
|
|
Yielding a second fixture value will get you an error.
|
|
|
|
It's possible we can evolve pytest to allow for producing
|
|
|
|
multiple values as an alternative to current parametrization.
|
|
|
|
For now, you can just use the normal
|
|
|
|
:ref:`fixture parametrization <fixture-parametrize>`
|
|
|
|
mechanisms together with ``yield``-style fixtures.
|
|
|
|
|
|
|
|
- lastly ``yield`` introduces more than one way to write
|
|
|
|
fixture functions, so what's the obvious way to a newcomer?
|
|
|
|
|
|
|
|
If you want to feedback or participate in discussion of the above
|
|
|
|
topics, please join our :ref:`contact channels`, you are most welcome.
|