Skip to content

Treating warnings as errors may interrupt teardown #9670

@PiotrBatko-BlockchainWares

Description

Description

Treating warnings as errors (e.g. with option -W error) may interrupt teardown. When warning is reported (with warnings.warn) during teardown, teardown is stopped. Teardown interruption is serious problem which leads to leak of resources (e.g. processes are not closed or temporary files are not deleted).

Expected behavior is teardown completion and then displaying warning message as error.

Minimal example

import warnings

import pytest


@pytest.fixture
def example_fixture():
    yield
    print('Teardown started')
    warnings.warn('Some warning')
    print('Teardown finished')


def test_teardown_bug(example_fixture):
    pass

Without -W error, teardown is fully executed correctly. (Note "Teardown finished" in output below).

$ pytest -s
======================================== test session starts ========================================
platform linux -- Python 3.8.12, pytest-7.0.0, pluggy-1.0.0
rootdir: /home/dev/Desktop/pytest-bug-reproduction
collected 1 item                                                                                    

test_teardown_bug.py .Teardown started
Teardown finished


========================================= warnings summary ==========================================
test_teardown_bug.py::test_teardown_bug
  /home/dev/Desktop/pytest-bug-reproduction/test_teardown_bug.py:10: UserWarning: Some warning
    warnings.warn('Some warning')

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=================================== 1 passed, 1 warning in 0.00s ====================================

When warnings are treated as errors, then teardown is interrupted. (Note that now "Teardown finished" is missing in output below).

$ pytest -s -W error
======================================== test session starts ========================================
platform linux -- Python 3.8.12, pytest-7.0.0, pluggy-1.0.0
rootdir: /home/dev/Desktop/pytest-bug-reproduction
collected 1 item                                                                                    

test_teardown_bug.py .Teardown started
E

============================================== ERRORS ===============================================
______________________________ ERROR at teardown of test_teardown_bug _______________________________

    @pytest.fixture
    def example_fixture():
        yield
        print('Teardown started')
>       warnings.warn('Some warning')
E       UserWarning: Some warning

test_teardown_bug.py:10: UserWarning
====================================== short test summary info ======================================
ERROR test_teardown_bug.py::test_teardown_bug - UserWarning: Some warning
==================================== 1 passed, 1 error in 0.01s =====================================

Environment

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.6 LTS
Release:	18.04
Codename:	bionic

$ python --version
Python 3.8.12

$ pytest --version
pytest 7.0.0

$ pip list
Package    Version
---------- -------
attrs      21.4.0
iniconfig  1.1.1
packaging  21.3
pip        22.0.3
pluggy     1.0.0
py         1.11.0
pyparsing  3.0.7
pytest     7.0.0
setuptools 56.0.0
tomli      2.0.1

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