test_ok2/testing/logging/test_fixture.py

71 lines
1.8 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
import logging
logger = logging.getLogger(__name__)
2017-10-25 07:01:00 +08:00
sublogger = logging.getLogger(__name__ + '.baz')
def test_fixture_help(testdir):
result = testdir.runpytest('--fixtures')
result.stdout.fnmatch_lines(['*caplog*'])
def test_change_level(caplog):
caplog.set_level(logging.INFO)
logger.debug('handler DEBUG level')
logger.info('handler INFO level')
caplog.set_level(logging.CRITICAL, logger=sublogger.name)
sublogger.warning('logger WARNING level')
sublogger.critical('logger CRITICAL level')
assert 'DEBUG' not in caplog.text
assert 'INFO' in caplog.text
assert 'WARNING' not in caplog.text
assert 'CRITICAL' in caplog.text
def test_with_statement(caplog):
with caplog.at_level(logging.INFO):
logger.debug('handler DEBUG level')
logger.info('handler INFO level')
with caplog.at_level(logging.CRITICAL, logger=sublogger.name):
sublogger.warning('logger WARNING level')
sublogger.critical('logger CRITICAL level')
assert 'DEBUG' not in caplog.text
assert 'INFO' in caplog.text
assert 'WARNING' not in caplog.text
assert 'CRITICAL' in caplog.text
def test_log_access(caplog):
logger.info('boo %s', 'arg')
assert caplog.records[0].levelname == 'INFO'
assert caplog.records[0].msg == 'boo %s'
assert 'boo arg' in caplog.text
def test_record_tuples(caplog):
logger.info('boo %s', 'arg')
assert caplog.record_tuples == [
(__name__, logging.INFO, 'boo arg'),
]
def test_unicode(caplog):
2017-09-27 05:27:38 +08:00
logger.info(u'')
assert caplog.records[0].levelname == 'INFO'
2017-09-27 05:27:38 +08:00
assert caplog.records[0].msg == u''
assert u'' in caplog.text
def test_clear(caplog):
2017-09-27 05:27:38 +08:00
logger.info(u'')
assert len(caplog.records)
caplog.clear()
assert not len(caplog.records)