PyDocs

Nav

Development Tools

TestCase.assertLogs

unittest

method of TestCase · Added in 3.4 (source)
  • TestCase.assertLogs(logger=None, level=None, formatter=None)
Fail unless a log message of level *level* or higher is emitted
on *logger_name* or its children.  If omitted, *level* defaults to
INFO and *logger* defaults to the root logger.

This method must be used as a context manager, and will yield
a recording object with two attributes: `output` and `records`.
At the end of the context manager, the `output` attribute will
be a list of the matching formatted log messages and the
`records` attribute will be a list of the corresponding LogRecord
objects.

Optionally supply `formatter` to control how messages are formatted.

Example::

    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'])
Changed in 3.15
  • 3.15: Now accepts a formatter to control how messages are formatted.
0 Examples
No examples for unittest/TestCase.assertLogs.
See Also
No see-alsos for unittest/TestCase.assertLogs
0 Notes
No notes for TestCase.assertLogs