Skip to content

Fixture teardown is called during setup of the next test #9287

@rnetser

Description

@rnetser

When having 2 tests which call module-scoped yield fixtures and using parametrize, teardown is done between tests, during the setup of the 2nd test (pytest_runtest_setup) and not during pytest_runtest_teardown.
Running with pytest 6.2.5
Is there a way to achieve the desired flow (i.e teardown is done when exiting the fixture, as described below in the flow without parametrize) while using parameterized?

Example test:

import pytest

import logging

LOGGER = logging.getLogger(__name__)


@pytest.fixture(scope="module")
def fix1():
    LOGGER.info("++++++++++++++++++++++++++fix1")
    yield
    LOGGER.info("t1")


@pytest.fixture(scope="module")
def fix2():
    LOGGER.info("++++++++++++++++++++++++++fix2")
    yield
    LOGGER.info("t2")


@pytest.mark.parametrize("fix1, fix2",
    [
    pytest.param(1, 2)
    ],
    indirect=True)
def test1(fix1, fix2):
    LOGGER.info("in test")


def test2(fix1, fix2):
    LOGGER.info("in test")

Output:

tests.test_ruty 2021-11-08 20:27:55 INFO ++++++++++++++++++++++++++fix1
tests.test_ruty 2021-11-08 20:27:55 INFO ++++++++++++++++++++++++++fix2
------------------------------------------------------------------------------------------------------ CALL ------------------------------------------------------------------------------------------------------
tests.test_ruty 2021-11-08 20:27:55 INFO in test

TEST: test1[1-2] STATUS: PASSED
.---------------------------------------------------------------------------------------------------- TEARDOWN ----------------------------------------------------------------------------------------------------

__________________________________________________________________ 1 of 2 completed, 1 Pass, 0 Fail, 0 Skip, 0 XPass, 0 XFail, 0 Error, 0 ReRun ___________________________________________________________________

tests/test_ruty.py 
------------------------------------------------------------------------------------------------------ test2 ------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------ SETUP ------------------------------------------------------------------------------------------------------
tests.test_ruty 2021-11-08 20:27:55 INFO t1
tests.test_ruty 2021-11-08 20:27:55 INFO ++++++++++++++++++++++++++fix1
tests.test_ruty 2021-11-08 20:27:55 INFO t2
tests.test_ruty 2021-11-08 20:27:55 INFO ++++++++++++++++++++++++++fix2
------------------------------------------------------------------------------------------------------ CALL ------------------------------------------------------------------------------------------------------
tests.test_ruty 2021-11-08 20:27:55 INFO in test

TEST: test2 STATUS: PASSED
.---------------------------------------------------------------------------------------------------- TEARDOWN ----------------------------------------------------------------------------------------------------
tests.test_ruty 2021-11-08 20:27:55 INFO t2
tests.test_ruty 2021-11-08 20:27:55 INFO t1

__________________________________________________________________ 2 of 2 completed, 2 Pass, 0 Fail, 0 Skip, 0 XPass, 0 XFail, 0 Error, 0 ReRun ___________________________________________________________________

When running without parametrize:

import pytest

import logging

LOGGER = logging.getLogger(__name__)


@pytest.fixture(scope="module")
def fix1():
    LOGGER.info("++++++++++++++++++++++++++fix1")
    yield
    LOGGER.info("t1")


@pytest.fixture(scope="module")
def fix2():
    LOGGER.info("++++++++++++++++++++++++++fix2")
    yield
    LOGGER.info("t2")


# @pytest.mark.parametrize("fix1, fix2",
#     [
#     pytest.param(1, 2)
#     ],
#     indirect=True)
def test1(fix1, fix2):
    LOGGER.info("in test")


def test2(fix1, fix2):
    LOGGER.info("in test")

Result:

tests.test_ruty 2021-11-08 20:37:23 INFO ++++++++++++++++++++++++++fix1
tests.test_ruty 2021-11-08 20:37:23 INFO ++++++++++++++++++++++++++fix2
------------------------------------------------------------------------------------------------------ CALL ------------------------------------------------------------------------------------------------------
tests.test_ruty 2021-11-08 20:37:23 INFO in test

TEST: test1 STATUS: PASSED
.---------------------------------------------------------------------------------------------------- TEARDOWN ----------------------------------------------------------------------------------------------------

__________________________________________________________________ 1 of 2 completed, 1 Pass, 0 Fail, 0 Skip, 0 XPass, 0 XFail, 0 Error, 0 ReRun ___________________________________________________________________

tests/test_ruty.py 
------------------------------------------------------------------------------------------------------ test2 ------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------ SETUP ------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------ CALL ------------------------------------------------------------------------------------------------------
tests.test_ruty 2021-11-08 20:37:23 INFO in test

TEST: test2 STATUS: PASSED
.---------------------------------------------------------------------------------------------------- TEARDOWN ----------------------------------------------------------------------------------------------------
tests.test_ruty 2021-11-08 20:37:23 INFO t2
tests.test_ruty 2021-11-08 20:37:23 INFO t1

__________________________________________________________________ 2 of 2 completed, 2 Pass, 0 Fail, 0 Skip, 0 XPass, 0 XFail, 0 Error, 0 ReRun ___________________________________________________________________

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytopic: parametrizerelated to @pytest.mark.parametrizetype: 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