summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIrit Katriel <iritkatriel@yahoo.com>2020-11-02 19:25:29 (GMT)
committerGitHub <noreply@github.com>2020-11-02 19:25:29 (GMT)
commit6fdfcec5b11f44f27aae3d53ddeb004150ae1f61 (patch)
treee9e669365f26bd39003be9b4e698959eb413881d
parentaca67da4fe68d5420401ac1782203d302875eb27 (diff)
downloadcpython-6fdfcec5b11f44f27aae3d53ddeb004150ae1f61.zip
cpython-6fdfcec5b11f44f27aae3d53ddeb004150ae1f61.tar.gz
cpython-6fdfcec5b11f44f27aae3d53ddeb004150ae1f61.tar.bz2
bpo-41943: Fix bug where assertLogs doesn't correctly filter messages… (GH-22565)
… by level @vsajip , @pitrou Automerge-Triggered-By: GH:vsajip
-rw-r--r--Lib/unittest/_log.py1
-rw-r--r--Lib/unittest/test/test_case.py12
-rw-r--r--Misc/NEWS.d/next/Library/2020-10-07-18-36-03.bpo-41943.Pt55fT.rst1
3 files changed, 14 insertions, 0 deletions
diff --git a/Lib/unittest/_log.py b/Lib/unittest/_log.py
index 961c448..94868e5 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 0e41696..b8aca92 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 0000000..3a7874d
--- /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