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

@ -1131,7 +1131,7 @@ Test cases
.. versionchanged:: 3.3
Added the *msg* keyword argument when used as a context manager.
.. method:: assertLogs(logger=None, level=None)
.. method:: assertLogs(logger=None, level=None, formatter=None)
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
@ -1146,6 +1146,10 @@ Test cases
its string equivalent (for example either ``"ERROR"`` or
:const:`logging.ERROR`). The default is :const:`logging.INFO`.
If given, *formatter* should be a :class:`logging.Formatter` object.
The default is a formatter with format string
``"%(levelname)s:%(name)s:%(message)s"``
The test passes if at least one message emitted inside the ``with``
block matches the *logger* and *level* conditions, otherwise it fails.
@ -1173,6 +1177,9 @@ Test cases
.. versionadded:: 3.4
.. versionchanged:: next
Now accepts a *formatter* to control how messages are formatted.
.. method:: assertNoLogs(logger=None, level=None)
A context manager to test that no messages are logged on

View file

@ -291,6 +291,15 @@ typing
(Contributed by Bénédikt Tran in :gh:`133823`.)
unittest
--------
* Lets users specify formatter in TestCase.assertLogs.
:func:`unittest.TestCase.assertLogs` will now accept a formatter
to control how messages are formatted.
(Contributed by Garry Cairns in :gh:`134567`.)
wave
----