disallow yield in non-yield-fixtures for now. This is an incompataibility but we want to prepare for possibly merging fixture and yield_fixture some day.

This commit is contained in:
holger krekel 2013-09-30 13:56:54 +02:00
parent 086d4e4ced
commit de35b077a2
3 changed files with 29 additions and 3 deletions

View File

@ -14,12 +14,18 @@ known incompatibilities:
- the pytest_plugin_unregister hook wasn't ever properly called
and there is no known implementation of the hook - so it got removed.
- pytest.fixture-decorated functions cannot be generators (i.e. use yield) anymore.
This change might be reversed in 2.4.1 if it causes unforeseen real-life issues.
However, you can always write and return an inner function/generator
and change the fixture consumer to iterate over the returned generator.
This change was done in lieu of the new ``pytest.yield_fixture`` decorator, see below.
new features:
- experimentally introduce a new pytest.yield_fixture decorator which
has exactly the same parameters as pytest.fixture but expects
- experimentally introduce a new ``pytest.yield_fixture`` decorator which
accepts exactly the same parameters as pytest.fixture but mandates
a ``yield`` statement instead of a ``return statement`` from fixture functions.
This allows direct integration with with-context managers
This allows direct integration with "with-style" context managers
in fixture functions and generally avoids registering of finalization callbacks
in favour of treating the "after-yield" as teardown code.
Thanks Andreas Pelme, Vladimir Keleshev, Floris Bruynooghe, Ronny Pfannschmidt

View File

@ -1705,6 +1705,11 @@ def call_fixture_func(fixturefunc, request, kwargs, yieldctx):
"yield_fixture function has more than one 'yield'")
request.addfinalizer(teardown)
else:
if is_generator(fixturefunc):
fail_fixturefunc(fixturefunc,
msg="pytest.fixture functions cannot use ``yield``. "
"Instead write and return an inner function/generator "
"and let the consumer call and iterate over it.")
res = fixturefunc(**kwargs)
return res

View File

@ -2087,3 +2087,18 @@ class TestContextManagerFixtureFuncs:
*def arg1*
""")
def test_yield_not_allowed_in_non_yield(self, testdir):
testdir.makepyfile("""
import pytest
@pytest.fixture(scope="module")
def arg1():
yield 1
def test_1(arg1):
pass
""")
result = testdir.runpytest("-s")
result.stdout.fnmatch_lines("""
*fixture*cannot use*yield*
*def arg1*
""")