Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pytest 8 broke sorting for package-scoped fixtures #12328

Closed
4 tasks done
bmerry opened this issue May 15, 2024 · 1 comment · Fixed by #12329
Closed
4 tasks done

Pytest 8 broke sorting for package-scoped fixtures #12328

bmerry opened this issue May 15, 2024 · 1 comment · Fixed by #12329
Labels
topic: collection related to the collection phase topic: fixtures anything involving fixtures directly or indirectly type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously

Comments

@bmerry
Copy link

bmerry commented May 15, 2024

Pytest 8 has a regression where the test reordering no longer favours reuse of package-scoped fixtures. If a parametrised package-scoped fixture is used in multiple modules within the same package, then instead of running the whole package with one fixture instantiation before switching to the next, it runs a module at a time, causing the fixture to be torn down and recreated each time.

MWE

conftest.py:

import pytest

@pytest.fixture(scope="package", params=["a", "b", "c"])
def fixture(request):
    return request.param

pkg/__init__.py: empty

pkg/test_things1.py:

def test1(fixture):
    pass

pkg/test_things2.py:

def test2(fixture):
    pass

Output of pytest --setup-plan from pytest 8.2.0:

======================================= test session starts ========================================
platform linux -- Python 3.12.3, pytest-8.2.0, pluggy-1.5.0
rootdir: /home/bmerry/work/experiments/NGC-1318-pytest-ordering/simple
collected 6 items                                                                                  

pkg/test_things1.py 
  SETUP    P fixture['a']
        pkg/test_things1.py::test1[a] (fixtures used: fixture, request)
  TEARDOWN P fixture['a']
  SETUP    P fixture['b']
        pkg/test_things1.py::test1[b] (fixtures used: fixture, request)
  TEARDOWN P fixture['b']
  SETUP    P fixture['c']
        pkg/test_things1.py::test1[c] (fixtures used: fixture, request)
pkg/test_things2.py 
  TEARDOWN P fixture['c']
  SETUP    P fixture['a']
        pkg/test_things2.py::test2[a] (fixtures used: fixture, request)
  TEARDOWN P fixture['a']
  SETUP    P fixture['b']
        pkg/test_things2.py::test2[b] (fixtures used: fixture, request)
  TEARDOWN P fixture['b']
  SETUP    P fixture['c']
        pkg/test_things2.py::test2[c] (fixtures used: fixture, request)
  TEARDOWN P fixture['c']

Whereas pytest 7.4.4 produces:

platform linux -- Python 3.12.3, pytest-7.4.4, pluggy-1.5.0
rootdir: /home/bmerry/work/experiments/NGC-1318-pytest-ordering/simple
collected 6 items                                                                                  

pkg/test_things1.py 
  SETUP    P fixture['a']
        pkg/test_things1.py::test1[a] (fixtures used: fixture, request)
pkg/test_things2.py 
        pkg/test_things2.py::test2[a] (fixtures used: fixture, request)
pkg/test_things1.py 
  TEARDOWN P fixture['a']
  SETUP    P fixture['b']
        pkg/test_things1.py::test1[b] (fixtures used: fixture, request)
pkg/test_things2.py 
        pkg/test_things2.py::test2[b] (fixtures used: fixture, request)
pkg/test_things1.py 
  TEARDOWN P fixture['b']
  SETUP    P fixture['c']
        pkg/test_things1.py::test1[c] (fixtures used: fixture, request)
pkg/test_things2.py 
        pkg/test_things2.py::test2[c] (fixtures used: fixture, request)
  TEARDOWN P fixture['c']

====================================== no tests ran in 0.01s =======================================

If the fixture is not parametric (or has only one parameter), it does correctly re-use the fixture across the whole package, so the issue seems to be related to sorting rather than re-use.

Changing the scope to session gives sensible behaviour, so it seems to be specific to package scope.

System information

pip list:

Package    Version
---------- -------
iniconfig  2.0.0
packaging  24.0
pip        24.0
pluggy     1.5.0
pytest     8.2.0
setuptools 69.5.1
wheel      0.43.0

OS: Ubuntu 24.04

  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible
@bluetech
Copy link
Member

I think I agree this is a regression. This broke with a21fb87 (released in 8.0.0). Specifically, the reorder_items function stopped reordering the items in this case. I will take a look.

@bluetech bluetech added type: bug problem that needs to be addressed topic: collection related to the collection phase type: regression indicates a problem that was introduced in a release which was working previously topic: fixtures anything involving fixtures directly or indirectly labels May 15, 2024
bluetech added a commit that referenced this issue May 15, 2024
The `.parent` was incorrectly removed in
a21fb87, but it was actually correct
(well, it assumes that Module.path.parent == Package.path, which I'm not
sure is always correct, but that's a different matter). Restore it.

Fix #12328.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: collection related to the collection phase topic: fixtures anything involving fixtures directly or indirectly type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants