Skip to content

Package fixtures teardown not being invoked after package scope #10992

Closed
@jvaesteves

Description

@jvaesteves

Hello, I'm currently trying to separate some package fixtures from some local conftest.py files. I know that this is not expected behaviour, but it actually works for my team setup. It look something something like this:

Python version: 3.11.3
Pytest version: 7.3.1

Project structure:

tests/__init__.py
tests/test_1/conftest.py
tests/test_1/__init__.py
tests/test_1/foo/__init__.py
tests/test_1/foo/test_foo.py
tests/test_1/fixtures/__init__.py
tests/test_1/fixtures/fix.py
tests/test_2/conftest.py
tests/test_2/fixtures/__init__.py
tests/test_2/fixtures/fix.py
tests/test_2/bar/__init__.py
tests/test_2/bar/test_bar.py

tests/test_1/fixtures/fix.py and tests/test_2/fixtures/fix.py

@pytest.fixture(scope='package')
def foo():
    # consider 'bar' for test_2/fixtures
    print('foo start')
    yield
    print('foo end')

tests/test_1/conftest.py and tests/test_2/conftest.py

from tests.test_1.fixtures.fix import foo
# consider 'bar' for test_1/conftest.py

tests/test_1/foo/test_foo.py and tests/test_2/bar/test_bar.py

def test_foo(foo):
    # consider 'bar' for tests/test_2/bar/test_bar.py
    print(foo)

When calling the fixtures in our tests with this modularisation, the fixture remains only available on the conftest.py level, and the startup of the fixture is also invoked by its first occurrence, but its teardown is deferred by beyond the end of the related package. Something like this:

test_1/foo/test_foo.py foo start
None
.
test_2/bar/test_bar.py bar start
None
.bar end
foo end

Is there any reason why this is happening and how can I enforce the package scope?

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytype: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions