make yielded tests participate in the autouse protocol

This commit is contained in:
holger krekel 2012-11-19 22:17:59 +01:00
parent d66ff7e63e
commit f263f54889
3 changed files with 29 additions and 8 deletions

View File

@ -1,6 +1,7 @@
Changes between 2.3.3 and 2.3.4.dev Changes between 2.3.3 and 2.3.4.dev
----------------------------------- -----------------------------------
- yielded tests will activate autouse-fixtures
- NOTE: the pre-2.0 way of yielding tests is not compatible - NOTE: the pre-2.0 way of yielding tests is not compatible
with autouse fixtures. If you need generative tests, use with autouse fixtures. If you need generative tests, use
@pytest.mark.parametrize or pytest_generate_tests, see the @pytest.mark.parametrize or pytest_generate_tests, see the

View File

@ -528,7 +528,7 @@ def hasinit(obj):
def fillfixtures(function): def fillfixtures(function):
""" fill missing funcargs for a test function. """ """ fill missing funcargs for a test function. """
if getattr(function, "_args", None) is None: # not a yielded function if 1 or getattr(function, "_args", None) is None: # not a yielded function
try: try:
request = function._request request = function._request
except AttributeError: except AttributeError:
@ -906,12 +906,15 @@ class Function(FunctionMixin, pytest.Item, FuncargnamesCompatAttr):
self.keywords[name] = val self.keywords[name] = val
fm = self.session._fixturemanager fm = self.session._fixturemanager
self._fixtureinfo = fi = fm.getfixtureinfo(self.parent, isyield = self._isyieldedfunction()
self.obj, self.cls) self._fixtureinfo = fi = fm.getfixtureinfo(self.parent, self.obj,
self.cls,
funcargs=not isyield)
self.fixturenames = fi.names_closure self.fixturenames = fi.names_closure
if self._isyieldedfunction(): if isyield:
assert not callspec, ( assert not callspec, (
"yielded functions (deprecated) cannot have funcargs") "yielded functions (deprecated) cannot have funcargs")
self.funcargs = {}
else: else:
if callspec is not None: if callspec is not None:
self.callspec = callspec self.callspec = callspec
@ -922,7 +925,6 @@ class Function(FunctionMixin, pytest.Item, FuncargnamesCompatAttr):
else: else:
self.funcargs = {} self.funcargs = {}
self._request = req = FixtureRequest(self) self._request = req = FixtureRequest(self)
#req._discoverfactories()
@property @property
def function(self): def function(self):
@ -1398,13 +1400,13 @@ class FixtureManager:
self._nodename2fixtureinfo = {} self._nodename2fixtureinfo = {}
def getfixtureinfo(self, node, func, cls): def getfixtureinfo(self, node, func, cls, funcargs=True):
key = (node, func.__name__) key = (node, func.__name__)
try: try:
return self._nodename2fixtureinfo[key] return self._nodename2fixtureinfo[key]
except KeyError: except KeyError:
pass pass
if not hasattr(node, "nofuncargs"): if funcargs and not hasattr(node, "nofuncargs"):
if cls is not None: if cls is not None:
startindex = 1 startindex = 1
else: else:

View File

@ -968,6 +968,24 @@ class TestAutouseManagement:
reprec = testdir.inline_run("-s") reprec = testdir.inline_run("-s")
reprec.assertoutcome(passed=1) reprec.assertoutcome(passed=1)
def test_autouse_honored_for_yield(self, testdir):
testdir.makepyfile("""
import pytest
@pytest.fixture(autouse=True)
def tst():
global x
x = 3
def test_gen():
def f(hello):
assert x == abs(hello)
yield f, 3
yield f, -3
""")
reprec = testdir.inline_run()
reprec.assertoutcome(passed=2)
def test_funcarg_and_setup(self, testdir): def test_funcarg_and_setup(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
import pytest import pytest