Improve error message when tests use closed stdout/stderr (capture)
See original GitHub issueThis 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_
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:29 (25 by maintainers)
yeah I’m fine with a better error message – somehow notify the user that they’re reassigning globals to
sys.std*
streams as a test side-effect@asottile The fixture works only when logging is set at function-level, when i define logging at module level, still tests fail with error https://github.com/pytest-dev/pytest/issues/5577