Skip to content

teardown incomplete if fixture raises an exception #314

@BerninaDeveloper

Description

@BerninaDeveloper

When there is a fixture which raises an exception the teardown of the module and session scoped fixtures is missing if reruns are used.
Usually pytest marks these tests as error (test is broken) but pytest still runs the session/module teardown.

How to reproduce (what I used):
python 3.12
pytest==8.4.2
pytest-rerunfailures==16.1

test_no_session_teardown.py:

import pytest


@pytest.fixture(scope="session", autouse=True)
def session_fixture():
    yield


@pytest.fixture(scope="module", autouse=True)
def module_fixture():
    yield


@pytest.fixture
def broken_fixture():
    yield
    raise Exception("anything")


@pytest.fixture
def any_fixture():
    yield


def test_fail_in_fixture(broken_fixture):
    pass


def test_ok(any_fixture):
    pass

Command without plugin: pytest --setup-show --tb=line .
Output is ok:

test_no_session_teardown.py
	SETUP    S session_fixture
		SETUP    M module_fixture
			SETUP    F broken_fixture
			test_no_session_teardown.py::test_fail_in_fixture (fixtures used: broken_fixture, module_fixture, session_fixture).
			TEARDOWN F broken_fixtureE
			SETUP    F any_fixture
			test_no_session_teardown.py::test_ok (fixtures used: any_fixture, module_fixture, session_fixture).
			TEARDOWN F any_fixture
		TEARDOWN M module_fixture
	TEARDOWN S session_fixture

	============================================================================ ERRORS ============================================================================
	__________________________________________________________ ERROR at teardown of test_fail_in_fixture ___________________________________________________________
	E   Exception: anything
	=================================================================== short test summary info ====================================================================
	ERROR test_no_session_teardown.py::test_fail_in_fixture - Exception: anything
	================================================================== 2 passed, 1 error in 0.02s ==================================================================

Command with plugin: pytest --setup-show . --tb=line --reruns=1
Output is missing module and session teardown:

	test_no_session_teardown.py
	SETUP    S session_fixture
		SETUP    M module_fixture
			SETUP    F broken_fixture
			test_no_session_teardown.py::test_fail_in_fixture (fixtures used: broken_fixture, module_fixture, session_fixture)
			TEARDOWN F broken_fixture.R
			SETUP    F broken_fixture
			test_no_session_teardown.py::test_fail_in_fixture (fixtures used: broken_fixture, module_fixture, session_fixture)
			TEARDOWN F broken_fixture.E
			SETUP    F any_fixture
			test_no_session_teardown.py::test_ok (fixtures used: any_fixture, module_fixture, session_fixture)
			TEARDOWN F any_fixture.

	============================================================================ ERRORS ============================================================================
	__________________________________________________________ ERROR at teardown of test_fail_in_fixture ___________________________________________________________
	E   Exception: anything
	=================================================================== short test summary info ====================================================================
	ERROR test_no_session_teardown.py::test_fail_in_fixture - Exception: anything
	============================================================= 3 passed, 1 error, 1 rerun in 0.02s ==============================================================

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughelp wanteda pull request to fix this issue is welcome

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions