Skip to content

fixture scope "package" wrong teardown order with nested packages #11480

@dszady-rtb

Description

@dszady-rtb

I have a following tests structure
image
with such fixtures:

# package_1/conftest.py
import pytest

@pytest.fixture(autouse=True, scope="package")
def some_fixture1():
    print("\n------------Setup package 1")

    yield

    print("\n------------ Teardown package 1")

and

# package_2/conftest.py

import pytest

@pytest.fixture(autouse=True, scope="package")
def some_fixture2():
    print("\n------------Setup package 2")

    yield

    print("\n------------Teardown package 2")

and tests

# package_1/nested_dir/test_package_1.py

def test_1():
    print("\n------------running test_1")
    assert True

and

# package_2/test_package_2.py

def test_1():
    print("\n------------running test_2")
    assert True

When I run the tests I would expect the package_1 teardown to happen before setup of package_2, but this is not what is happening. Instead, teardown happens at the end. See output:
image

But when I move the test file out of the nested_dir so the file structure looks like this:
image
the teardown order seems to be correct.
image

Anyways I would love to have a correct teardown order for nested tests file structure.

Pip list output:

(venv) ➜  pythonProject pip list          
Package    Version
---------- -------
iniconfig  2.0.0
packaging  23.2
pip        22.3.1
pluggy     1.3.0
pytest     7.4.2
setuptools 65.5.1
wheel      0.38.4

Operating system:
Macos Ventura 13.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectly

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions