Skip to content

Autouse and parameterization results in fixtures not being torn down, or even being torn down out of order  #6436

@SalmonMode

Description

@SalmonMode
$ pip list --format=columns Package Version ------------------ ------- attrs 19.3.0 importlib-metadata 1.3.0 more-itertools 8.0.2 packaging 20.0 pip 9.0.1 pkg-resources 0.0.0 pluggy 0.13.1 py 1.8.1 pyparsing 2.4.6 pytest 5.3.2 selenium 3.141.0 setuptools 39.0.1 six 1.13.0 urllib3 1.25.7 wcwidth 0.1.8 zipp 0.6.0 

I noticed that when autouse and parameterized fixtures of multiple scope levels, it can result in fixtures not being torn down every time a parameterized fixture is torn down if they were executed after the parameterized fixture was setup (and before it was torn down). I also saw in more extreme cases that fixtures would even be torn down in the wrong order.

Here's a simple example:

import pytest @pytest.fixture(scope="module", params=["m1", "m2"], autouse=True) def m_fix(request): yield @pytest.fixture(scope="class") def c_fix(): yield class TestFixtures: def test_a(self, c_fix): pass

I would expect the setup plan for this to be:

tests/test_auto.py SETUP M m_fix[m1] SETUP C c_fix tests/test_auto.py::TestFixtures::test_a[m1] (fixtures used: c_fix, m_fix, request) TEARDOWN C c_fix TEARDOWN M m_fix[m1] SETUP M m_fix[m2] SETUP C c_fix tests/test_auto.py::TestFixtures::test_a[m2] (fixtures used: c_fix, m_fix, request) TEARDOWN C c_fix TEARDOWN M m_fix[m2] 

but it's actually this:

tests/test_auto.py SETUP M m_fix[m1] SETUP C c_fix tests/test_auto.py::TestFixtures::test_a[m1] (fixtures used: c_fix, m_fix, request) TEARDOWN M m_fix[m1] SETUP M m_fix[m2] tests/test_auto.py::TestFixtures::test_a[m2] (fixtures used: c_fix, m_fix, request) TEARDOWN C c_fix TEARDOWN M m_fix[m2] 

Notice that the class-level fixture is not torn down along with the parameterized, autouse module-level fixture, despite being affected by it.

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