bpo-41943: Fix bug where assertLogs doesn't correctly filter messages… (GH-22565)

… by level

@vsajip , @pitrou

Automerge-Triggered-By: GH:vsajip
This commit is contained in:
Irit Katriel 2020-11-02 19:25:29 +00:00 committed by GitHub
parent aca67da4fe
commit 6fdfcec5b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 0 deletions

View File

@ -47,6 +47,7 @@ class _AssertLogsContext(_BaseTestCaseContext):
logger = self.logger = logging.getLogger(self.logger_name)
formatter = logging.Formatter(self.LOGGING_FORMAT)
handler = _CapturingHandler()
handler.setLevel(self.level)
handler.setFormatter(formatter)
self.watcher = handler.watcher
self.old_handlers = logger.handlers[:]

View File

@ -1673,6 +1673,18 @@ test case
with self.assertLogs(level='WARNING'):
log_foo.info("1")
def testAssertLogsFailureLevelTooHigh_FilterInRootLogger(self):
# Failure due to level too high - message propagated to root
with self.assertNoStderr():
oldLevel = log_foo.level
log_foo.setLevel(logging.INFO)
try:
with self.assertRaises(self.failureException):
with self.assertLogs(level='WARNING'):
log_foo.info("1")
finally:
log_foo.setLevel(oldLevel)
def testAssertLogsFailureMismatchingLogger(self):
# Failure due to mismatching logger (and the logged message is
# passed through)

View File

@ -0,0 +1 @@
Fix bug where TestCase.assertLogs doesn't correctly filter messages by level.