pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Test listed twice when there's an error during teardown

Open The-Compiler opened this issue 10 years ago • 8 comments

With this code:

import pytest

@pytest.yield_fixture
def fixt():
    yield
    raise Exception

def test_foo(fixt):
    pass

We get this output:

collected 1 items 

py/test_foo.py::test_foo PASSED
py/test_foo.py::test_foo ERROR

================================== ERRORS ===================================
_______________________ ERROR at teardown of test_foo _______________________

    @pytest.yield_fixture
    def fixt():
        yield
>       raise Exception
E       Exception

py/test_foo.py:6: Exception
===================== 1 passed, 1 error in 0.41 seconds ====================

The test obviously only should be listed once, and no passed tests should be listed.

The-Compiler avatar Sep 15 '15 05:09 The-Compiler

it's actually intentional -- each phase out of setup/call/teardown gets its own report (so each test item has three reports) but passed setup/teardown reports are surpressed usually.

hpk42 avatar Sep 15 '15 12:09 hpk42

the ux is confusing that way, can we have one printed report per item so bascially print the call report when teardown passes, not when call passes?

RonnyPfannschmidt avatar Sep 15 '15 13:09 RonnyPfannschmidt

As a user, I think some improvements could be made in this area. I can understand instances where users may want to vary how this happens--and I don't think that this means they should have to implement a bunch of hooks to get what they want.

Currently, it seems inconsistent. You don't get PASSED for the setup step that worked, you always get the call step result, and you only get errors from teardown. (By the way, I love how failures and errors are differentiated personally.)

Perhaps not exactly the same issue but I've also noticed how you can print things from setup but finalizers seem to execute outside of the scope that captures output. If print works in setup, it should work in teardown.

moorecm avatar Oct 07 '15 20:10 moorecm

What's also a consequence of this - and is pretty annoying with long outputs - is that they're printed twice if there's an issue both during call and teardown:

import pytest

@pytest.yield_fixture
def fixt():
    yield
    raise Exception

def test_foo(fixt):
    print("Hello World")
    raise ValueError
=========================== ERRORS ===========================
_______________ ERROR at teardown of test_foo ________________
pytest-test.py:7: in fixt
    raise Exception
E   Exception
-------------------- Captured stdout call --------------------
Hello World
========================== FAILURES ==========================
__________________________ test_foo __________________________
pytest-test.py:12: in test_foo
    raise ValueError
E   ValueError
-------------------- Captured stdout call --------------------
Hello World

Should I open a separate issue for that?

The-Compiler avatar Dec 17 '15 10:12 The-Compiler

Is this getting fixed ?

mevinbabuc avatar Aug 15 '21 11:08 mevinbabuc

I don't think this will get fixed actually as it happens by design.

nicoddemus avatar Aug 15 '21 12:08 nicoddemus

If it's not getting fixed, shouldn't it be closed? As it is now, it's tagged with the 'help wanted' tag, which is why I even looked.

jeffwright13 avatar May 01 '22 05:05 jeffwright13

it should get fixed, we should print the test passed not after the call, but after the teardown to avoid the double print

RonnyPfannschmidt avatar May 01 '22 11:05 RonnyPfannschmidt