-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
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_handlersShould 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 #14 (comment)