Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Autouse fixtures with the same name in class and module scopes are executed in the wrong order #11281

@david-shiko

Description

@david-shiko

Key parts from the docs:
quote 1:
Autouse fixtures are executed first within their scope
quote 2:
Higher-scoped fixtures are executed first

According to this docs module level setup should be executed before class level setup, but:

Correct behavior (fixtures has different names):

import pytest


@pytest.fixture(scope='module', autouse=True, )
def module_setup():
    print()
    print(1)


class TestFoo:

    @classmethod
    @pytest.fixture(scope='class', autouse=True, )
    def class_setup(cls, ):
        print()
        print(2)

    def test_class(self):
        """without this test cls fixture will not be executed at all"""


def test_module():  # Intentionally at the end
    """without this test module fixture will not be executed at all"""

Output:

(venv) david@david-comp:~/.config/JetBrains/PyCharmCE2023.2/scratches$ pytest -s scratch_4.py 
=============================================== test session starts ================================================
platform linux -- Python 3.10.12, pytest-7.4.0, pluggy-1.2.0
rootdir: /home/david/.config/JetBrains/PyCharmCE2023.2/scratches
plugins: profiling-1.7.0, Faker-19.2.0, postgresql-5.0.0
collected 2 items                                                                                                  

scratch_4.py 
1

2
..

================================================ 2 passed in 0.02s =================================================
(venv) david@david-comp:~/.config/JetBrains/PyCharmCE2023.2/scratches$ 

incorrect / undocumentated behavior (fixtures has the same names):

import pytest


@pytest.fixture(scope='module', autouse=True, )
def setup():
    print()
    print(1)

class TestFoo:

    @classmethod
    @pytest.fixture(scope='class', autouse=True, )
    def setup(cls, ):
        print()
        print(2)

    def test_class(self):
        """without this test cls fixture will not be executed at all"""


def test_module():  # Intentionally at the end
    """without this test module fixture will not be executed at all"""

Output:

(venv) david@david-comp:~/.config/JetBrains/PyCharmCE2023.2/scratches$ pytest -s scratch_4.py 
=============================================== test session starts ================================================
platform linux -- Python 3.10.12, pytest-7.4.0, pluggy-1.2.0
rootdir: /home/david/.config/JetBrains/PyCharmCE2023.2/scratches
plugins: profiling-1.7.0, Faker-19.2.0, postgresql-5.0.0
collected 2 items                                                                                                  

scratch_4.py 
2
.
1
.

================================================ 2 passed in 0.03s =================================================
(venv) david@david-comp:~/.config/JetBrains/PyCharmCE2023.2/scratches$ 

Bonuses:
Placing some test before TestFoo cls will restore the order execution to the proper one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions