Description
Thanks for pytest -- it's amazing, and makes testing so much better in Python. I really appreciate both the work you've done and the top-notch docs. It's incredible.
I've run into a problem that may be pilot error -- perhaps I misunderstand pytest and logging? If so, I'd appreciate a pointer on how to do this properly.
A (somewhat) detailed description of the bug or problem
I wrote code in a fixture to record setup errors in a child fixture using the Python logger. I found that these logs aren't reported by pytest, and I don't know why. In the minimal example below, fix1
uses a finally
clause to log information about errors produced by fix2
. The finally
clause's code is executed, but the log entry is suppressed. Is there a way for pytest to display these "missing" logs?
Specifically, the text below displays pytest_bug.py Efix1 capture of post-setup failures
(showing the fix1 finally
clause runs) but the log output doesn't contain are not reported by the pytest logger
.
Minimal example
import logging
import pytest
logger = logging.getLogger(__name__)
@pytest.fixture
def fix1():
logger.error("fix1 setup")
try:
yield
finally:
print("fix1 capture of post-setup failures")
logger.error("are not reported by the pytest logger")
@pytest.fixture
def fix2(fix1):
raise IndexError
def test_1(fix2):
pass
The output for me; I used the -s
flag so the print
statement shows up.
(venv_tmp) C:\blah>pytest pytest_bug.py -s
========================================= test session starts =========================================
platform win32 -- Python 3.9.1, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: C:\Users\bjones\Documents\git
collected 1 item
pytest_bug.py Efix1 capture of post-setup failures
=============================================== ERRORS ================================================
______________________________________ ERROR at setup of test_1 _______________________________________
fix1 = None
@pytest.fixture
def fix2(fix1):
> raise IndexError
E IndexError
pytest_bug.py:19: IndexError
----------------------------------------- Captured log setup ------------------------------------------
ERROR pytest_bug:pytest_bug.py:9 fix1 setup
======================================= short test summary info =======================================
ERROR pytest_bug.py::test_1 - IndexError
========================================== 1 error in 0.13s ===========================================
Output of pip list
from the virtual environment you are using
(venv_tmp) C:\blah>pip list
Package Version
------------ -------
atomicwrites 1.4.0
attrs 21.2.0
colorama 0.4.4
iniconfig 1.1.1
packaging 21.0
pip 21.2.4
pluggy 0.13.1
py 1.10.0
pyparsing 2.4.7
pytest 6.2.4
setuptools 49.2.1
toml 0.10.2
Pytest and operating system versions
- platform win32 -- Python 3.9.1, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
- Win 10 x64 Version 10.0.19043 Build 19043