pytest
pytest copied to clipboard
Test listed twice when there's an error during teardown
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.
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.
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?
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.
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?
Is this getting fixed ?
I don't think this will get fixed actually as it happens by design.
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.
it should get fixed, we should print the test passed not after the call, but after the teardown to avoid the double print