pytest-dev / pytest-dev/pytest
Log entries not reported
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the minimal example in pytest_bug.py with pytest 6.2.4 and compare the fixture setup and teardown logging around fix1 and fix2. Trace pytest's logging capture during fixture teardown; done means the post-setup logger.error entry is reported alongside the existing captured setup log.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100