pytest-dev / pytest-dev/pytest
Improve error message when tests use closed stdout/stderr (capture)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Description
@nicoddemus @asottile
This is still an issue with the latest pytest across tests (e.g. when one tests change the logging config and add a stream handler pointing to stdout, a subsequent test that doesn't setup logging and tries to write to logging will fail).
I think the issue boils down to the fact that if you set up a Stream Handler and you enable capsys the actual output stream will be a pytest stream that will be closed and thrown away at the next test in the test suite. So when a subsequent test tries to write to it you get this error. Adding the following fixture fixed this:
LOGGER = logging.getLogger()
@pytest.fixture(autouse=True)
def ensure_logging_framework_not_altered():
before_handlers = list(LOGGER.handlers)
yield
LOGGER.handlers = before_handlers
Should this be automatically done always by pytest? Granted for this to work ideally you would need to do it for all loggers, not just the root one. In this case, for me, this was enough as I was only set up a custom stream logger onto the root logger.
In an ideal world, I would expect though pytest to cleanup loggig configurations at the end tests that requested the caplog fixture.
Originally posted by @gaborbernat in https://github.com/pytest-dev/pytest/issues/14#issuecomment-521577819
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 tracing the capsys and caplog fixture setup and teardown, focusing on how captured streams and logging handlers are closed between tests. Reproduce the failure with a StreamHandler targeting stdout, then verify that subsequent logging does not write to a closed pytest stream and that the relevant logging configuration is cleaned up after the test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100