ID EN
Unittest

assertLogs

Python 3.11

A context manager to test that at least one message is logged on the logger or one of its children, with at least the given level.

Syntax

PYTHON
assertLogs(logger=None, level=None)

Examples

Example 1
PYTHON
with self.assertLogs('foo', level='INFO') as cm:
    logging.getLogger('foo').info('first message')
    logging.getLogger('foo.bar').error('second message')
self.assertEqual(cm.output, ['INFO:foo:first message',
                             'ERROR:foo.bar:second message'])

See Also

logging.Logger str logging.ERROR logging.INFO logging.LogRecord str