-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Open
Description
Related: #12876 #5743 #5502 #5282 and probably a lot more
OS: Windows
Python: 3.13.7
pytest: 9.0.2
Calling logging.basicConfig() before pytest.main() and then using logging.info/warn/... inside a test will add lengthy, noisy error logs to the test's stderr capture. This noise is an obstacle in test debugging.
Minimal working example
Run the following in PowerShell:
python --version
# Python 3.13.7
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install pytest
pip show pytest
# Version: 9.0.2
@"
import logging, pytest
def test_pass():
logging.warning('warn')
if __name__ == '__main__':
logging.basicConfig()
pytest.main()
"@ | Out-File -FilePath "test_main.py" -Encoding utf8
@"
import logging, pytest
@pytest.hookimpl(trylast=True)
def pytest_runtest_logreport(report):
logging.warning(report.capstderr)
"@ | Out-File -FilePath "conftest.py" -Encoding utf8
python test_main.py
# --- Logging error ---
# Traceback (most recent call last):
# File "C:\Users\ABrun\.pyenv\pyenv-win\versions\3.13.7\Lib\logging\__init__.py", line 1154, in emit
# stream.write(msg + self.terminator)
# ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
# OSError: [WinError 6] The handle is invalidThe package python-html is using the pytest_runtest_logreport hook to generate the html output:
# https://github.com/pytest-dev/pytest-html/blob/e73b6befd69f1852ec1369ef6faab5b1f0286c8e/src/pytest_html/basereport.py#L210-L212
@pytest.hookimpl(trylast=True)
def pytest_runtest_logreport(self, report):MWE with pytest-html:
python --version
# Python 3.13.7
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install pytest pytest-html
pip show pytest
# Version: 9.0.2
pip show pytest-html
# Version: 4.2.0
@"
import logging, pytest
def test_pass():
logging.warning('warn')
if __name__ == '__main__':
logging.basicConfig()
pytest.main(['--html=out.html'])
"@ | Out-File -FilePath "test_main.py" -Encoding utf8
python test_main.py
python -c "import re; print(re.search(r';log(.*?-.*?)#34', open('out.html').read()).group(1).replace('\\n', '\n'))"
# --- Logging error ---
# Traceback (most recent call last):
# File "C:\Users\ABrun\.pyenv\pyenv-win\versions\3.13.7\Lib\logging\__init__.py", line 1154, in emit
# stream.write(msg + self.terminator)
# ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
# OSError: [WinError 6] The handle is invalidA workaround would be appreciated. I have tried the ones from the other issues but they did not prevent that error message. The comment #13720 (comment) mentions caplog. Is there a way to use caplog to avoid that error message?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels