pyln: Remove any logging handlers at teardown to avoid logging error

Inspired by https://github.com/pytest-dev/pytest/issues/5502#issuecomment-647157873
This commit is contained in:
Christian Decker 2020-10-07 10:41:36 +02:00
parent 9f8c9d6406
commit d5d41ef1a0

View File

@ -40,14 +40,25 @@ def test_base_dir():
@pytest.fixture(autouse=True)
def setup_logging():
logger = logging.getLogger()
before_handlers = list(logger.handlers)
"""Enable logging before a test, and remove all handlers afterwards.
This "fixes" the issue with pytest swapping out sys.stdout and sys.stderr
in order to capture the output, but then doesn't wait for the handlers to
terminate before closing the buffers. It just iterates through all
loggers, and removes any handlers that might be pointing at sys.stdout or
sys.stderr.
"""
if TEST_DEBUG:
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
yield
logger.handlers = before_handlers
loggers = [logging.getLogger()] + list(logging.Logger.manager.loggerDict.values())
for logger in loggers:
handlers = getattr(logger, 'handlers', [])
for handler in handlers:
logger.removeHandler(handler)
@pytest.fixture