diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 891e071c6..1ecd9310e 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -597,14 +597,16 @@ class Package(Module): if path.basename == "__init__.py" and path.dirpath() == this_path: continue + for pkg_prefix in pkg_prefixes: + if pkg_prefix in path.parts() and pkg_prefix.join('__init__.py') != path: + skip = True + + if skip: + continue + if path.isdir() and path.join('__init__.py').check(file=1): pkg_prefixes.add(path) - for pkg_prefix in pkg_prefixes: - if pkg_prefix in path.parts() and pkg_prefix.join('__init__.py') == path: - skip = True - if skip: - continue for x in self._collectfile(path): yield x diff --git a/testing/python/fixture.py b/testing/python/fixture.py index bbfcf775b..4170f8f37 100644 --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -3979,3 +3979,71 @@ class TestScopeOrdering(object): items, _ = testdir.inline_genitems() request = FixtureRequest(items[0]) assert request.fixturenames == "s1 p1 m1 m2 c1 f2 f1".split() + + def test_multiple_packages(self, testdir): + """Complex test involving multiple package fixtures. Make sure teardowns + are executed in order. + . + └── root + ├── __init__.py + ├── sub1 + │ ├── __init__.py + │ ├── conftest.py + │ └── test_1.py + └── sub2 + ├── __init__.py + ├── conftest.py + └── test_2.py + """ + root = testdir.mkdir("root") + root.join("__init__.py").write("values = []") + sub1 = root.mkdir("sub1") + sub1.ensure("__init__.py") + sub1.join("conftest.py").write( + dedent( + """\ + import pytest + from .. import values + @pytest.fixture(scope="package") + def fix(): + values.append("pre-sub1") + yield values + values.pop() + """ + ) + ) + sub1.join("test_1.py").write( + dedent( + """\ + from .. import values + def test_1(fix): + assert values == ["pre-sub1"] + """ + ) + ) + sub2 = root.mkdir("sub2") + sub2.ensure("__init__.py") + sub2.join("conftest.py").write( + dedent( + """\ + import pytest + from .. import values + @pytest.fixture(scope="package") + def fix(): + values.append("pre-sub2") + yield values + values.pop() + """ + ) + ) + sub2.join("test_2.py").write( + dedent( + """\ + from .. import values + def test_2(fix): + assert values == ["pre-sub2"] + """ + ) + ) + reprec = testdir.inline_run() + reprec.assertoutcome(passed=2)