bpo-39385: Add an assertNoLogs context manager to unittest.TestCase (GH-18067)

Co-authored-by: Rémi Lapeyre <remi.lapeyre@henki.fr>
This commit is contained in:
Kit Choi 2020-07-01 22:08:38 +01:00 committed by GitHub
parent 5d5c84ef78
commit 6b34d7b51e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 131 additions and 8 deletions

View file

@ -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.