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
(self, logger=None, level=None, formatter=None)
| 850 | return context.handle('_assertNotWarns', args, kwargs) |
| 851 | |
| 852 | def assertLogs(self, logger=None, level=None, formatter=None): |
| 853 | """Fail unless a log message of level *level* or higher is emitted |
| 854 | on *logger_name* or its children. If omitted, *level* defaults to |
| 855 | INFO and *logger* defaults to the root logger. |
| 856 | |
| 857 | This method must be used as a context manager, and will yield |
| 858 | a recording object with two attributes: `output` and `records`. |
| 859 | At the end of the context manager, the `output` attribute will |
| 860 | be a list of the matching formatted log messages and the |
| 861 | `records` attribute will be a list of the corresponding LogRecord |
| 862 | objects. |
| 863 | |
| 864 | Optionally supply `formatter` to control how messages are formatted. |
| 865 | |
| 866 | Example:: |
| 867 | |
| 868 | with self.assertLogs('foo', level='INFO') as cm: |
| 869 | logging.getLogger('foo').info('first message') |
| 870 | logging.getLogger('foo.bar').error('second message') |
| 871 | self.assertEqual(cm.output, ['INFO:foo:first message', |
| 872 | 'ERROR:foo.bar:second message']) |
| 873 | """ |
| 874 | # Lazy import to avoid importing logging if it is not needed. |
| 875 | from ._log import _AssertLogsContext |
| 876 | return _AssertLogsContext(self, logger, level, no_logs=False, formatter=formatter) |
| 877 | |
| 878 | def assertNoLogs(self, logger=None, level=None): |
| 879 | """ Fail unless no log messages of level *level* or higher are emitted |