diff --git a/CHANGELOG b/CHANGELOG index 75ff8927b..3e584493f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/_pytest/python.py b/_pytest/python.py index cc7b5ba20..4c1437c79 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -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: diff --git a/testing/python/fixture.py b/testing/python/fixture.py index a1afe9f57..35d53c3b0 100644 --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -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