From 6fdfcec5b11f44f27aae3d53ddeb004150ae1f61 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Mon, 2 Nov 2020 19:25:29 +0000 Subject: [PATCH] =?UTF-8?q?bpo-41943:=20Fix=20bug=20where=20assertLogs=20d?= =?UTF-8?q?oesn't=20correctly=20filter=20messages=E2=80=A6=20(GH-22565)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … by level @vsajip , @pitrou Automerge-Triggered-By: GH:vsajip --- Lib/unittest/_log.py | 1 + Lib/unittest/test/test_case.py | 12 ++++++++++++ .../Library/2020-10-07-18-36-03.bpo-41943.Pt55fT.rst | 1 + 3 files changed, 14 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-10-07-18-36-03.bpo-41943.Pt55fT.rst diff --git a/Lib/unittest/_log.py b/Lib/unittest/_log.py index 961c448a7fb..94868e5bb95 100644 --- a/Lib/unittest/_log.py +++ b/Lib/unittest/_log.py @@ -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[:] diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py index 0e416967a30..b8aca92a8eb 100644 --- a/Lib/unittest/test/test_case.py +++ b/Lib/unittest/test/test_case.py @@ -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) diff --git a/Misc/NEWS.d/next/Library/2020-10-07-18-36-03.bpo-41943.Pt55fT.rst b/Misc/NEWS.d/next/Library/2020-10-07-18-36-03.bpo-41943.Pt55fT.rst new file mode 100644 index 00000000000..3a7874d25ae --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-10-07-18-36-03.bpo-41943.Pt55fT.rst @@ -0,0 +1 @@ +Fix bug where TestCase.assertLogs doesn't correctly filter messages by level. \ No newline at end of file