Skip to content

Package fixtures teardown not being invoked after package scope #10992

Closed

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?

Activity

  1. jvaesteves commented on May 12, 2023

    @jvaesteves
    Author

    To add more details: I'm using fixtures to mock some datasets as Spark tables. What I do is to write the data in a temporary directory, saving it as a table in the Spark metastore in the startup and dropping it during the teardown.

    But due to this behaviour of package fixtures, I get an error when invoking a different fixture pointing to the same table on another test as the table is not being dropped before its recreation.

    I also just tried to create a local conftest.py with all needed fixtures (instead of importing them) for different packages to double check if the error actually happens because of the modularisation, but I still get a "TableAlreadyExists" exception indicating that the fixture teardown is not being invoked before the table is recreated.

  2. changed the title [-]Package scope fixtures out of order when trying to separate fixtures in different files[/-] [+]Package fixtures teardown not being invoked after package scope[/+] on May 12, 2023
  3. RonnyPfannschmidt commented on May 12, 2023

    @RonnyPfannschmidt
    Member

    This is most likely related to the way package scope fixtures where initially created

    It's not well defined what package they belong to

    It's also unclear if a easy way to fix it exists

  4. added
    type: questiongeneral question, might be closed after 2 weeks of inactivity
    topic: fixturesanything involving fixtures directly or indirectly
    on May 16, 2023
  5. bluetech commented on Jan 4, 2024

    @bluetech
    Member

    We have made a lot of changes to Package support in #11646. Please try pip install pytest==8.0.0rc1 and let us know if things still don't work as you expect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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