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
-----------------------------------
- yielded tests will activate autouse-fixtures
- NOTE: the pre-2.0 way of yielding tests is not compatible
with autouse fixtures. If you need generative tests, use
@pytest.mark.parametrize or pytest_generate_tests, see the

View File

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

View File

@ -968,6 +968,24 @@ class TestAutouseManagement:
reprec = testdir.inline_run("-s")
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):
testdir.makepyfile("""
import pytest