gh-134567: Add the formatter parameter in unittest.TestCase.assertLogs (GH-134570)

This commit is contained in:
Garry Cairns 2025-07-02 10:51:19 +01:00 committed by GitHub
parent b19c9da401
commit 51ab66b3d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 42 additions and 5 deletions

View file

@ -849,7 +849,7 @@ class TestCase(object):
context = _AssertNotWarnsContext(expected_warning, self)
return context.handle('_assertNotWarns', args, kwargs)
def assertLogs(self, logger=None, level=None):
def assertLogs(self, 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.
@ -861,6 +861,8 @@ class TestCase(object):
`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:
@ -871,7 +873,7 @@ class TestCase(object):
"""
# Lazy import to avoid importing logging if it is not needed.
from ._log import _AssertLogsContext
return _AssertLogsContext(self, logger, level, no_logs=False)
return _AssertLogsContext(self, logger, level, no_logs=False, formatter=formatter)
def assertNoLogs(self, logger=None, level=None):
""" Fail unless no log messages of level *level* or higher are emitted