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:
parent
086d4e4ced
commit
de35b077a2
12
CHANGELOG
12
CHANGELOG
|
@ -14,12 +14,18 @@ known incompatibilities:
|
||||||
- the pytest_plugin_unregister hook wasn't ever properly called
|
- the pytest_plugin_unregister hook wasn't ever properly called
|
||||||
and there is no known implementation of the hook - so it got removed.
|
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:
|
new features:
|
||||||
|
|
||||||
- experimentally introduce a new pytest.yield_fixture decorator which
|
- experimentally introduce a new ``pytest.yield_fixture`` decorator which
|
||||||
has exactly the same parameters as pytest.fixture but expects
|
accepts exactly the same parameters as pytest.fixture but mandates
|
||||||
a ``yield`` statement instead of a ``return statement`` from fixture functions.
|
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 fixture functions and generally avoids registering of finalization callbacks
|
||||||
in favour of treating the "after-yield" as teardown code.
|
in favour of treating the "after-yield" as teardown code.
|
||||||
Thanks Andreas Pelme, Vladimir Keleshev, Floris Bruynooghe, Ronny Pfannschmidt
|
Thanks Andreas Pelme, Vladimir Keleshev, Floris Bruynooghe, Ronny Pfannschmidt
|
||||||
|
|
|
@ -1705,6 +1705,11 @@ def call_fixture_func(fixturefunc, request, kwargs, yieldctx):
|
||||||
"yield_fixture function has more than one 'yield'")
|
"yield_fixture function has more than one 'yield'")
|
||||||
request.addfinalizer(teardown)
|
request.addfinalizer(teardown)
|
||||||
else:
|
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)
|
res = fixturefunc(**kwargs)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
|
@ -2087,3 +2087,18 @@ class TestContextManagerFixtureFuncs:
|
||||||
*def arg1*
|
*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*
|
||||||
|
""")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue