Kaydet (Commit) 7f71e04c authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka

Issue #24134: assertRaises() and assertRaisesRegexp() checks are not longer

successful if the callable is None.

Added tests for assertRaises().
üst 3234abb9
...@@ -127,6 +127,8 @@ class _AssertRaisesContext(object): ...@@ -127,6 +127,8 @@ class _AssertRaisesContext(object):
(expected_regexp.pattern, str(exc_value))) (expected_regexp.pattern, str(exc_value)))
return True return True
def _sentinel(*args, **kwargs):
raise AssertionError('Should never called')
class TestCase(object): class TestCase(object):
"""A class whose instances are single test cases. """A class whose instances are single test cases.
...@@ -443,7 +445,7 @@ class TestCase(object): ...@@ -443,7 +445,7 @@ class TestCase(object):
return '%s : %s' % (safe_repr(standardMsg), safe_repr(msg)) return '%s : %s' % (safe_repr(standardMsg), safe_repr(msg))
def assertRaises(self, excClass, callableObj=None, *args, **kwargs): def assertRaises(self, excClass, callableObj=_sentinel, *args, **kwargs):
"""Fail unless an exception of class excClass is raised """Fail unless an exception of class excClass is raised
by callableObj when invoked with arguments args and keyword by callableObj when invoked with arguments args and keyword
arguments kwargs. If a different type of exception is arguments kwargs. If a different type of exception is
...@@ -451,7 +453,7 @@ class TestCase(object): ...@@ -451,7 +453,7 @@ class TestCase(object):
deemed to have suffered an error, exactly as for an deemed to have suffered an error, exactly as for an
unexpected exception. unexpected exception.
If called with callableObj omitted or None, will return a If called with callableObj omitted, will return a
context object used like this:: context object used like this::
with self.assertRaises(SomeException): with self.assertRaises(SomeException):
...@@ -467,7 +469,7 @@ class TestCase(object): ...@@ -467,7 +469,7 @@ class TestCase(object):
self.assertEqual(the_exception.error_code, 3) self.assertEqual(the_exception.error_code, 3)
""" """
context = _AssertRaisesContext(excClass, self) context = _AssertRaisesContext(excClass, self)
if callableObj is None: if callableObj is _sentinel:
return context return context
with context: with context:
callableObj(*args, **kwargs) callableObj(*args, **kwargs)
...@@ -973,7 +975,7 @@ class TestCase(object): ...@@ -973,7 +975,7 @@ class TestCase(object):
self.fail(self._formatMessage(msg, standardMsg)) self.fail(self._formatMessage(msg, standardMsg))
def assertRaisesRegexp(self, expected_exception, expected_regexp, def assertRaisesRegexp(self, expected_exception, expected_regexp,
callable_obj=None, *args, **kwargs): callable_obj=_sentinel, *args, **kwargs):
"""Asserts that the message in a raised exception matches a regexp. """Asserts that the message in a raised exception matches a regexp.
Args: Args:
...@@ -987,7 +989,7 @@ class TestCase(object): ...@@ -987,7 +989,7 @@ class TestCase(object):
if expected_regexp is not None: if expected_regexp is not None:
expected_regexp = re.compile(expected_regexp) expected_regexp = re.compile(expected_regexp)
context = _AssertRaisesContext(expected_exception, self, expected_regexp) context = _AssertRaisesContext(expected_exception, self, expected_regexp)
if callable_obj is None: if callable_obj is _sentinel:
return context return context
with context: with context:
callable_obj(*args, **kwargs) callable_obj(*args, **kwargs)
......
...@@ -954,6 +954,50 @@ test case ...@@ -954,6 +954,50 @@ test case
self.assertRaises(self.failureException, self.assertRegexpMatches, self.assertRaises(self.failureException, self.assertRegexpMatches,
'saaas', r'aaaa') 'saaas', r'aaaa')
def testAssertRaisesCallable(self):
class ExceptionMock(Exception):
pass
def Stub():
raise ExceptionMock('We expect')
self.assertRaises(ExceptionMock, Stub)
# A tuple of exception classes is accepted
self.assertRaises((ValueError, ExceptionMock), Stub)
# *args and **kwargs also work
self.assertRaises(ValueError, int, '19', base=8)
# Failure when no exception is raised
with self.assertRaises(self.failureException):
self.assertRaises(ExceptionMock, lambda: 0)
# Failure when the function is None
with self.assertRaises(TypeError):
self.assertRaises(ExceptionMock, None)
# Failure when another exception is raised
with self.assertRaises(ExceptionMock):
self.assertRaises(ValueError, Stub)
def testAssertRaisesContext(self):
class ExceptionMock(Exception):
pass
def Stub():
raise ExceptionMock('We expect')
with self.assertRaises(ExceptionMock):
Stub()
# A tuple of exception classes is accepted
with self.assertRaises((ValueError, ExceptionMock)) as cm:
Stub()
# The context manager exposes caught exception
self.assertIsInstance(cm.exception, ExceptionMock)
self.assertEqual(cm.exception.args[0], 'We expect')
# *args and **kwargs also work
with self.assertRaises(ValueError):
int('19', base=8)
# Failure when no exception is raised
with self.assertRaises(self.failureException):
with self.assertRaises(ExceptionMock):
pass
# Failure when another exception is raised
with self.assertRaises(ExceptionMock):
self.assertRaises(ValueError, Stub)
def testAssertRaisesRegexp(self): def testAssertRaisesRegexp(self):
class ExceptionMock(Exception): class ExceptionMock(Exception):
pass pass
...@@ -964,6 +1008,8 @@ test case ...@@ -964,6 +1008,8 @@ test case
self.assertRaisesRegexp(ExceptionMock, re.compile('expect$'), Stub) self.assertRaisesRegexp(ExceptionMock, re.compile('expect$'), Stub)
self.assertRaisesRegexp(ExceptionMock, 'expect$', Stub) self.assertRaisesRegexp(ExceptionMock, 'expect$', Stub)
self.assertRaisesRegexp(ExceptionMock, u'expect$', Stub) self.assertRaisesRegexp(ExceptionMock, u'expect$', Stub)
with self.assertRaises(TypeError):
self.assertRaisesRegexp(ExceptionMock, 'expect$', None)
def testAssertNotRaisesRegexp(self): def testAssertNotRaisesRegexp(self):
self.assertRaisesRegexp( self.assertRaisesRegexp(
......
...@@ -21,6 +21,9 @@ Core and Builtins ...@@ -21,6 +21,9 @@ Core and Builtins
Library Library
------- -------
- Issue #24134: assertRaises() and assertRaisesRegexp() checks are not longer
successful if the callable is None.
- Issue #23008: Fixed resolving attributes with boolean value is False in pydoc. - Issue #23008: Fixed resolving attributes with boolean value is False in pydoc.
- Issues #24099, #24100, and #24101: Fix free-after-use bug in heapq's siftup - Issues #24099, #24100, and #24101: Fix free-after-use bug in heapq's siftup
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment