From 846cf781a13ba3a98c7e3c64c18f5d0386846884 Mon Sep 17 00:00:00 2001 From: Anatoly Bubenkov Date: Tue, 12 Nov 2013 13:45:36 +0100 Subject: [PATCH] use deepest fixturedef for fixture closure --HG-- branch : multi-level-fixture-deps-override --- _pytest/python.py | 3 +-- testing/test_conftest.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/_pytest/python.py b/_pytest/python.py index 4c742c668..085e445be 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -1556,8 +1556,7 @@ class FixtureManager: fixturedefs = self.getfixturedefs(argname, parentid) arg2fixturedefs[argname] = fixturedefs if fixturedefs is not None: - for fixturedef in fixturedefs: - merge(fixturedef.argnames) + merge(fixturedefs[-1].argnames) return fixturenames_closure, arg2fixturedefs def pytest_generate_tests(self, metafunc): diff --git a/testing/test_conftest.py b/testing/test_conftest.py index 6f2dc2619..c2d5ff1c6 100644 --- a/testing/test_conftest.py +++ b/testing/test_conftest.py @@ -215,3 +215,40 @@ def test_conftest_import_order(testdir, monkeypatch): conftest = Conftest() monkeypatch.setattr(conftest, 'importconftest', impct) assert conftest.getconftestmodules(sub) == [ct1, ct2] + + +def test_fixture_dependency(testdir, monkeypatch): + ct1 = testdir.makeconftest("") + ct1 = testdir.makepyfile("__init__.py") + ct1.write("") + sub = testdir.mkdir("sub") + sub.join("__init__.py").write("") + sub.join("conftest.py").write(py.std.textwrap.dedent(""" + import pytest + + @pytest.fixture + def not_needed(): + assert False, "Should not be called!" + + @pytest.fixture + def foo(): + assert False, "Should not be called!" + + @pytest.fixture + def bar(foo): + return 'bar' + """)) + subsub = sub.mkdir("subsub") + subsub.join("__init__.py").write("") + subsub.join("test_bar.py").write(py.std.textwrap.dedent(""" + import pytest + + @pytest.fixture + def bar(): + return 'sub bar' + + def test_event_fixture(bar): + assert bar == 'sub bar' + """)) + result = testdir.runpytest("sub") + result.stdout.fnmatch_lines(["*1 passed*"])