Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When assertions fail inside a test, contextmanagers don't work #3365

Closed
parantapa opened this issue Apr 3, 2018 · 3 comments
Closed

When assertions fail inside a test, contextmanagers don't work #3365

parantapa opened this issue Apr 3, 2018 · 3 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@parantapa
Copy link

Description of the bug

When assertions fail inside a test, context managers don't work.

Test code

from contextlib import contextmanager
@contextmanager
def hello():
    print("this runs")
    yield
    print("this doesn't run")

def test_hello():
    with hello():
        assert 1 == 0

Running the above code with pytest -s yields

> pytest -s
======================== test session starts ========================
platform linux -- Python 3.6.4, pytest-3.5.0, py-1.5.3, pluggy-0.6.0
rootdir: /home/parantapa/workspace/pytest_test, inifile:
collected 1 item                                                    

test_basic.py this runs
F

============================= FAILURES ==============================
____________________________ test_hello _____________________________

    def test_hello():
        with hello():
>           assert 1 == 0
E           assert 1 == 0

test_basic.py:11: AssertionError
===================== 1 failed in 0.03 seconds ======================

pip list of the virtual environment

pip list
attrs (17.4.0)
more-itertools (4.1.0)
pip (9.0.3)
pluggy (0.6.0)
py (1.5.3)
pytest (3.5.0)
setuptools (39.0.1)
six (1.11.0)
wheel (0.31.0)

pytest and operating system versions

pytest=3.5.0, python=3.6, OS=ArchLinux

@pytestbot
Copy link
Contributor

GitMate.io thinks possibly related issues are #2121 (Test assertions does not work properly when specifying python_files setting), #747 (flakes test fail), #662 (fail on zero tests found), #1777 (sigalrm fails test), and #2172 (Capturing output causes test to fail.).

@pytestbot pytestbot added the type: bug problem that needs to be addressed label Apr 3, 2018
@nicoddemus
Copy link
Member

For the code block after yield to execute in case of an exception you need to surround it with try/except:

from contextlib import contextmanager
@contextmanager
def hello():
    print("this runs")
    try:
        yield
    finally:
        print("this doesn't run")

def test_hello():
    with hello():
        assert 1 == 0

This is how @contextmanager works and not really related to pytest.

@nicoddemus nicoddemus added type: question general question, might be closed after 2 weeks of inactivity and removed type: bug problem that needs to be addressed labels Apr 4, 2018
@RonnyPfannschmidt
Copy link
Member

closing as this one is entirely about core python behaviour

schopin-pro added a commit to schopin-pro/apport that referenced this issue Jul 21, 2023
Our context manager improperly failed to deal with exceptions thrown in
the block under context, resulting in the test process never being
killed.

See pytest-dev/pytest#3365
bdrung pushed a commit to canonical/apport that referenced this issue Jul 21, 2023
Our context manager improperly failed to deal with exceptions thrown in
the block under context, resulting in the test process never being
killed.

See pytest-dev/pytest#3365
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

4 participants