Access captures logs in teardown

This commit is contained in:
Anders Hovmöller
2018-01-15 15:01:01 +01:00
parent 01e37fe892
commit 7ea5a22657
5 changed files with 44 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import logging
import pytest
logger = logging.getLogger(__name__)
sublogger = logging.getLogger(__name__ + '.baz')
@@ -68,3 +69,23 @@ def test_clear(caplog):
assert len(caplog.records)
caplog.clear()
assert not len(caplog.records)
@pytest.fixture
def logging_during_setup_and_teardown(caplog):
logger.info('a_setup_log')
yield
logger.info('a_teardown_log')
assert [x.message for x in caplog.get_handler('teardown').records] == ['a_teardown_log']
def test_caplog_captures_for_all_stages(caplog, logging_during_setup_and_teardown):
assert not caplog.records
assert not caplog.get_handler('call').records
logger.info('a_call_log')
assert [x.message for x in caplog.get_handler('call').records] == ['a_call_log']
assert [x.message for x in caplog.get_handler('setup').records] == ['a_setup_log']
# This reachers into private API, don't use this type of thing in real tests!
assert set(caplog._item.catch_log_handlers.keys()) == {'setup', 'call'}