pytest-dev / pytest-dev/pytest

8.1.1 removed ability to introspect teardown exceptions in `pytest_fixture_post_finalizer`

Open
#12,306 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: fixtures type: regression
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

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: https://github.com/pytest-dev/pytest/commit/434282e17f5f1f4fcc1464a0a0921cf19804bdd7#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 (https://github.com/pytest-dev/pytest/compare/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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing pytest 8.0.2 and 8.1.1 around the linked commit, using the minimal conftest.py and test_repro.py examples as a reproduction. Trace the pytest_fixture_post_finalizer call and add regression coverage for the reported sys.exc_info() behavior; done means the teardown exception is observable as described or the supported alternative is clearly established.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.