Skip to content

Conversation

@Yuheng3107
Copy link
Contributor

@Yuheng3107 Yuheng3107 commented Jan 17, 2026

Closes #89900

Description

The Problem

At the moment, assertLogs removes the handlers attached to the logger. In general this is good, because it reduces message spamming in the test logs. However, if the code being tested is relying on a handler to do something, then the test fails because the handler is being removed. This leads to the situation that the same exact test must be run twice:

  • first time within the context manager, to assert that specific messages were logged (using with self.assertLogs())
  • second time, without the assertLogs to ensure the code that uses a handler does the right thing

The Fix

Added optional keep_handlers param in assertLogs inside Lib/unittest/case.py to optionally preserve existing handlers for assertLogs, preventing the situation described above where the same exact test must be run twice

Since keep_handlers is False by default, this change is backwards-compatible.

Tests

Added new test cases in Lib/test/test_unittest to ensure keep_handlers=True preserves existing handlers.

Validation

Ran ./python.exe -m test test_unittest and passed all 1093 tests


📚 Documentation preview 📚: https://cpython-previews--143970.org.readthedocs.build/

…tLogs

added documentation for new keep_handlers kwarg for assertLogs function in unittest
@Yuheng3107
Copy link
Contributor Author

@vsajip Could you review this PR when you have time? Thanks so much!

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not say that I was ok with the request so for now, please do not open PRs without explicit approval.

logger.addHandler(handler)
else:
logger.handlers = [handler]
logger.propagate = False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thie sets propagate before the level but it was not case previously.

@@ -0,0 +1,2 @@
Add *keep_handlers* parameter to :meth:`unittest.TestCase.assertLogs` to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would require more than just a NEWS entry. We would also need a What's New.

The default is a formatter with format string
``"%(levelname)s:%(name)s:%(message)s"``

If given, *keep_handlers* should be a boolean value. If ``True``,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we were to add an additional parameter I think a keyword-only parameter is better.

The default is a formatter with format string
``"%(levelname)s:%(name)s:%(message)s"``

If given, *keep_handlers* should be a boolean value. If ``True``,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is overly verbose.Usually if the default is a boolean we assume that the reader is smart enough to infer that the type is a boolean (though at runtime we just use truthiness)

Added the *keep_handlers* parameter to optionally preserve
existing handlers.

.. method:: assertNoLogs(logger=None, level=None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For instance we should also allow a keep_handlers keyword here.

@bedevere-app
Copy link

bedevere-app bot commented Jan 18, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

assertLogs to optionally not disable existing handlers

2 participants