summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/test/test_case.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/unittest/test/test_case.py')
-rw-r--r--Lib/unittest/test/test_case.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py
index 3dedcbe..0e41696 100644
--- a/Lib/unittest/test/test_case.py
+++ b/Lib/unittest/test/test_case.py
@@ -1681,6 +1681,81 @@ test case
with self.assertLogs('foo'):
log_quux.error("1")
+ def testAssertLogsUnexpectedException(self):
+ # Check unexpected exception will go through.
+ with self.assertRaises(ZeroDivisionError):
+ with self.assertLogs():
+ raise ZeroDivisionError("Unexpected")
+
+ def testAssertNoLogsDefault(self):
+ with self.assertRaises(self.failureException) as cm:
+ with self.assertNoLogs():
+ log_foo.info("1")
+ log_foobar.debug("2")
+ self.assertEqual(
+ str(cm.exception),
+ "Unexpected logs found: ['INFO:foo:1']",
+ )
+
+ def testAssertNoLogsFailureFoundLogs(self):
+ with self.assertRaises(self.failureException) as cm:
+ with self.assertNoLogs():
+ log_quux.error("1")
+ log_foo.error("foo")
+
+ self.assertEqual(
+ str(cm.exception),
+ "Unexpected logs found: ['ERROR:quux:1', 'ERROR:foo:foo']",
+ )
+
+ def testAssertNoLogsPerLogger(self):
+ with self.assertNoStderr():
+ with self.assertLogs(log_quux):
+ with self.assertNoLogs(logger=log_foo):
+ log_quux.error("1")
+
+ def testAssertNoLogsFailurePerLogger(self):
+ # Failure due to unexpected logs for the given logger or its
+ # children.
+ with self.assertRaises(self.failureException) as cm:
+ with self.assertLogs(log_quux):
+ with self.assertNoLogs(logger=log_foo):
+ log_quux.error("1")
+ log_foobar.info("2")
+ self.assertEqual(
+ str(cm.exception),
+ "Unexpected logs found: ['INFO:foo.bar:2']",
+ )
+
+ def testAssertNoLogsPerLevel(self):
+ # Check per-level filtering
+ with self.assertNoStderr():
+ with self.assertNoLogs(level="ERROR"):
+ log_foo.info("foo")
+ log_quux.debug("1")
+
+ def testAssertNoLogsFailurePerLevel(self):
+ # Failure due to unexpected logs at the specified level.
+ with self.assertRaises(self.failureException) as cm:
+ with self.assertNoLogs(level="DEBUG"):
+ log_foo.debug("foo")
+ log_quux.debug("1")
+ self.assertEqual(
+ str(cm.exception),
+ "Unexpected logs found: ['DEBUG:foo:foo', 'DEBUG:quux:1']",
+ )
+
+ def testAssertNoLogsUnexpectedException(self):
+ # Check unexpected exception will go through.
+ with self.assertRaises(ZeroDivisionError):
+ with self.assertNoLogs():
+ raise ZeroDivisionError("Unexpected")
+
+ def testAssertNoLogsYieldsNone(self):
+ with self.assertNoLogs() as value:
+ pass
+ self.assertIsNone(value)
+
def testDeprecatedMethodNames(self):
"""
Test that the deprecated methods raise a DeprecationWarning. See #9424.