pytest icon indicating copy to clipboard operation
pytest copied to clipboard

8.1.1 removed ability to introspect teardown exceptions in `pytest_fixture_post_finalizer`

Open mchase-nasuni opened this issue 1 year ago • 3 comments

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

mchase-nasuni avatar May 10 '24 15:05 mchase-nasuni

The same issue is raised in the Test Explorer running the latest version of Visual Studio, all tests in the explorer are now Skipped due to:

C:\dev\my_project\venv\Scripts\python.exe C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\2022\COMMUNITY\COMMON7\IDE\EXTENSIONS\MICROSOFT\PYTHON\CORE\PythonFiles\testing_tools\run_adapter.py discover pytest --output-file C:\Users\axa\AppData\Local\Temp\tmpA5E6.tmp -- --cache-clear --rootdir=C:\dev\my_project
ERROR: failed to patch pytest, _pytest.compat._translate_non_printable

(With the suggestion to use an older version of Pytest: https://github.com/microsoft/PTVS/issues/7853)

axelande avatar May 21 '24 07:05 axelande

@axelande This is completely unrelated, and happens because PTVS is accessing private pytest API which was moved in 3ba40954007d56b0f0cef251660c127fe6540137.

The-Compiler avatar May 21 '24 08:05 The-Compiler

I'd be happy to accept a PR based on your branch to restore this behavior 🙂

Zac-HD avatar Jul 06 '24 17:07 Zac-HD