Skip to content

8.1.1 removed ability to introspect teardown exceptions in pytest_fixture_post_finalizer #12306

Open
@mchase-nasuni

Description

@mchase-nasuni

From several years ago until recently (works in 8.0.2, doesn't work in 8.1.1), it was possible to get teardown exception information in pytest_fixture_post_finalizer by calling sys.exc_info(). This appears to have changed with the following commit: 434282e#diff-49027cfd80e14edac9b0fae71f7228a408a09599f66a7815839ce8c3ae2ab84fL1020-R1043, which moved the hook call from inside the finally block to before the exception is (re)raised.

Perhaps there's another way to get at this information, but I wasn't able to find it by reading documentation, searching the web, or trying to read the source code. I was able to make things work again by somewhat rearranging the new code.

This was first encountered in CI images that I believe were based off of Ubuntu 22.04, but I was able to reproduce on macOS Monterey.

Here is the code for my attempt at a minimal example:

conftest.py

import sys

import pytest


@pytest.fixture(autouse=True)
def _fail_eventually():
    yield
    raise RuntimeError


def pytest_fixture_post_finalizer():
    exc_info = sys.exc_info()
    if exc_info != (None, None, None):
        print("Saw exception!")
    else:
        print("Didn't see exception")

test_repro.py

def test_pass():
    pass

Against 8.0.2, this prints Saw exception!, and pip list has the following output:

Package    Version
---------- -------
iniconfig  2.0.0
packaging  24.0
pip        24.0
pluggy     1.5.0
pytest     8.0.2
setuptools 65.5.0

Against 8.1.1, this prints Didn't see exception, and pip list has the following output:

Package    Version
---------- -------
iniconfig  2.0.0
packaging  24.0
pip        24.0
pluggy     1.5.0
pytest     8.1.1
setuptools 65.5.0

Against 8.2.0, this still prints Didn't see exception, and pip list has the following output:

Package    Version
---------- -------
iniconfig  2.0.0
packaging  24.0
pip        24.0
pluggy     1.5.0
pytest     8.2.0
setuptools 65.5.0

Against a branch I created in a fork of pytest to check that I'm right about this issue (main...mchase-nasuni:pytest:mchase-nasuni-internal-testing), this prints Saw exception!, and pip list has the following output:

Package    Version
---------- ---------------------
iniconfig  2.0.0
packaging  24.0
pip        24.0
pluggy     1.5.0
pytest     0.1.dev15909+g0692015
setuptools 65.5.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytype: regressionindicates a problem that was introduced in a release which was working previously

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions