cpython: 4f5815747f58 (original) (raw)

Mercurial > cpython

changeset 85701:4f5815747f58

Issue #18937: Add an assertLogs() context manager to unittest.TestCase to ensure that a block of code emits a message using the logging module. [#18937]

Antoine Pitrou solipsis@pitrou.net
date Sat, 14 Sep 2013 19:45:47 +0200
parents 9354325d2ee4
children 1dc925ee441a
files Doc/library/unittest.rst Lib/unittest/case.py Lib/unittest/test/test_case.py Misc/NEWS
diffstat 4 files changed, 242 insertions(+), 6 deletions(-)[+] [-] Doc/library/unittest.rst 41 Lib/unittest/case.py 109 Lib/unittest/test/test_case.py 96 Misc/NEWS 2

line wrap: on

line diff

--- a/Doc/library/unittest.rst +++ b/Doc/library/unittest.rst @@ -1031,6 +1031,47 @@ Test cases .. versionchanged:: 3.3 Added the msg keyword argument when used as a context manager.

+

+

+

+

+

+

+

+

+

+

+

+ There are also other methods used to perform more specific checks, such as:

--- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -3,6 +3,7 @@ import sys import functools import difflib +import logging import pprint import re import warnings @@ -115,10 +116,21 @@ def expectedFailure(test_item): return test_item -class _AssertRaisesBaseContext(object): +class _BaseTestCaseContext: +

+

+ + +class _AssertRaisesBaseContext(_BaseTestCaseContext): def init(self, expected, test_case, callable_obj=None, expected_regex=None):

@@ -133,10 +145,6 @@ class _AssertRaisesBaseContext(object): self.expected_regex = expected_regex self.msg = None

- def handle(self, name, callable_obj, args, kwargs): """ If callable_obj is None, assertRaises/Warns is being used as a @@ -150,7 +158,6 @@ class _AssertRaisesBaseContext(object): callable_obj(*args, *kwargs) - class _AssertRaisesContext(_AssertRaisesBaseContext): """A context manager used to implement TestCase.assertRaises methods.""" @@ -232,6 +239,74 @@ class _AssertWarnsContext(_AssertRaisesB self._raiseFailure("{} not triggered".format(exc_name)) + +_LoggingWatcher = collections.namedtuple("_LoggingWatcher",

+ + +class _CapturingHandler(logging.Handler):

+

+

+

+ + + +class _AssertLogsContext(_BaseTestCaseContext):

+

+

+

+

+ + class TestCase(object): """A class whose instances are single test cases. @@ -644,6 +719,28 @@ class TestCase(object): context = _AssertWarnsContext(expected_warning, self, callable_obj) return context.handle('assertWarns', callable_obj, args, kwargs)

+

+

+

+ def _getAssertEqualityFunc(self, first, second): """Get a detailed comparison function for the types of the two args.

--- a/Lib/unittest/test/test_case.py +++ b/Lib/unittest/test/test_case.py @@ -1,8 +1,10 @@ +import contextlib import difflib import pprint import pickle import re import sys +import logging import warnings import weakref import inspect @@ -16,6 +18,12 @@ from .support import ( TestEquality, TestHashing, LoggingResult, LegacyLoggingResult, ResultWithNoStartTestRunStopTestRun ) +from test.support import captured_stderr + + +log_foo = logging.getLogger('foo') +log_foobar = logging.getLogger('foo.bar') +log_quux = logging.getLogger('quux') class Test(object): @@ -1251,6 +1259,94 @@ test case with self.assertWarnsRegex(RuntimeWarning, "o+"): _runtime_warn("barz")

+

+

+

+

+

+

+

+

+

+

+ def testDeprecatedMethodNames(self): """ Test that the deprecated methods raise a DeprecationWarning. See #9424.

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,8 @@ Core and Builtins Library ------- +- Issue #18937: Add an assertLogs() context manager to unittest.TestCase