Skip to content

Count teardown error as fail #13585

Closed
Closed
@ego-thales

Description

@ego-thales

Manually imported and completed from here (wrong repo, my bad).


Hi everyone,

Consider this:

import pytest

@pytest.fixture
def foo():
    yield
    raise

def test_a(): raise
def test_b(foo): ...

It provides this report:

$ pytest [...]/tmp.py
=============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.13.3, pytest-8.4.1, pluggy-1.6.0
rootdir: ********
plugins: cov-6.2.1
collected 2 items                                                                                                                                                                                                 

[...]/tmp.py F.E                                                                                                                                                                                  [100%]

===================================================================================================== ERRORS ======================================================================================================
___________________________________________________________________________________________ ERROR at teardown of test_b ___________________________________________________________________________________________

    @pytest.fixture
    def foo():
        yield
>       raise
E       RuntimeError: No active exception to reraise

[...]/tmp.py:6: RuntimeError
==================================================================================================== FAILURES =====================================================================================================
_____________________________________________________________________________________________________ test_a ______________________________________________________________________________________________________

>   def test_a(): raise
                  ^^^^^
E   RuntimeError: No active exception to reraise

[...]/tmp.py:8: RuntimeError
============================================================================================= short test summary info =============================================================================================
FAILED [...]/tmp.py::test_a - RuntimeError: No active exception to reraise
ERROR [...]/tmp.py::test_b - RuntimeError: No active exception to reraise
====================================================================================== 1 failed, 1 passed, 1 error in 0.07s =======================================================================================
$

As you can see, test_b PASSED but there was an error in teardown of foo. Is it possible to make it so that foo fails test_b? I tried pytest.fail() but it behaves the same.

If it is not possible, I think it would be nice to consider! Indeed, the teardown can be used to match console output with capsys for example., As such, a match_outerr fixture may want to report teardown error as a test failure specifically. I think it should still be an option, si by default, fixture teardown errors may not be related to any test itself failing. However, I get that it is not that straightforward sin fixture may have scopes lrager than "function".

All the best.
Élie.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs informationreporter needs to provide more information; can be closed after 2 or more weeks of inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions