testutil: add assertLogs wrapper that enables/disables logs

pull/687/head
Ryan Barrett 2023-10-16 10:36:43 -07:00
rodzic 66cf3bfd42
commit 7466a000a5
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
1 zmienionych plików z 19 dodań i 0 usunięć

Wyświetl plik

@ -1,4 +1,5 @@
"""Common test utility code."""
import contextlib
import copy
from datetime import datetime
import logging
@ -480,3 +481,21 @@ class TestCase(unittest.TestCase, testutil.Asserts):
def assert_equals(self, expected, actual, msg=None, ignore=(), **kwargs):
return super().assert_equals(
expected, actual, msg=msg, ignore=tuple(ignore) + ('@context',), **kwargs)
@contextlib.contextmanager
def assertLogs(self):
"""Wraps :meth:`unittest.TestCase.assertLogs` and enables/disables logs.
Works around ``oauth_dropins.webutil.tests.__init__``.
"""
orig_disable_level = logging.root.manager.disable
logging.disable(logging.NOTSET)
try:
with super().assertLogs() as logs:
yield logs
finally:
logging.disable(orig_disable_level)
# emit logs that were captured
for record in logs.records:
logging.getLogger().handle(record)