New in version 2.4.
pytest-2.4 allows fixture functions to seamlessly use a yield instead of a return statement to provide a fixture value while otherwise fully supporting all other fixture features.
“yielding” fixture values is an experimental feature and its exact declaration may change later but earliest in a 2.5 release. You can thus safely use this feature in the 2.4 series but may need to adapt later. Test functions themselves will not need to change (as a general feature, they are ignorant of how fixtures are setup).
Let’s look at a simple standalone-example using the new yield syntax:
# content of test_yield.py import pytest @pytest.yield_fixture 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 finalization through registering callbacks, 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 1 passed in 0.00 seconds
We can also seemlessly use the new syntax with with statements. Let’s simplify the above passwd fixture:
# content of test_yield2.py import pytest @pytest.yield_fixture 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.
Note that the new syntax is fully integrated with using scope, params and other fixture features. Changing existing fixture functions to use yield is thus straight forward.
The yield-syntax has been discussed by pytest users extensively. In general, the advantages of the using a yield fixture syntax are:
However, there are also limitations or foreseeable irritations:
If you want to feedback or participate in the ongoing discussion, please join our Contact channels. you are most welcome.