Skip to content

An exception from pytest_fixture_post_finalizer prevents fixture from being torn down completely #14114

@Anton3

Description

@Anton3

Example conftest.py:

import pytest

@pytest.hookimpl(trylast=True)
def pytest_fixture_post_finalizer(request):
    if "test1" in request.node.nodeid:
        raise RuntimeError("error")

Tests:

import pytest

@pytest.fixture
def fixture(request):
    return request.node.nodeid

def test1(fixture):
    assert "test1" in fixture

def test2(fixture):
    assert "test2" in fixture

Expected: test1 error, test2 passed.
Actual: test1 error, test2 failed.

Running with --setup-show shows:

test_what.py::test1 
        SETUP    F fixture
        test_what.py::test1 (fixtures used: fixture, request)PASSED
        TEARDOWN F fixture
test_what.py::test1 ERROR
test_what.py::test2 
        test_what.py::test2 (fixtures used: fixture, request)FAILED

Every time a fixture tries to teardown, pytest_fixture_post_finalizer can just say "nope", and the fixture will have the same value in the next test, causing hard-to-debug errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions