cpython: 5418ab3e5556 (original) (raw)
--- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -129,15 +129,17 @@ class _BaseTestCaseContext: msg = self.test_case._formatMessage(self.msg, standardMsg) raise self.test_case.failureException(msg) +def _sentinel(*args, **kwargs):
class _AssertRaisesBaseContext(_BaseTestCaseContext):
- def init(self, expected, test_case, callable_obj=_sentinel, expected_regex=None): _BaseTestCaseContext.init(self, test_case) self.expected = expected self.test_case = test_case
if callable_obj is not None:[](#l1.18)
if callable_obj is not _sentinel:[](#l1.19) try:[](#l1.20) self.obj_name = callable_obj.__name__[](#l1.21) except AttributeError:[](#l1.22)
@@ -151,11 +153,11 @@ class _AssertRaisesBaseContext(_BaseTest def handle(self, name, callable_obj, args, kwargs): """
If callable_obj is None, assertRaises/Warns is being used as a[](#l1.27)
If callable_obj is _sentinel, assertRaises/Warns is being used as a[](#l1.28) context manager, so check for a 'msg' kwarg and return self.[](#l1.29)
If callable_obj is not None, call it passing args and kwargs.[](#l1.30)
If callable_obj is not _sentinel, call it passing args and kwargs.[](#l1.31) """[](#l1.32)
if callable_obj is None:[](#l1.33)
if callable_obj is _sentinel:[](#l1.34) self.msg = kwargs.pop('msg', None)[](#l1.35) return self[](#l1.36) with self:[](#l1.37)
@@ -674,7 +676,7 @@ class TestCase(object): except UnicodeDecodeError: return '%s : %s' % (safe_repr(standardMsg), safe_repr(msg))
- def assertRaises(self, excClass, callableObj=_sentinel, *args, **kwargs): """Fail unless an exception of class excClass is raised by callableObj when invoked with arguments args and keyword arguments kwargs. If a different type of exception is
@@ -682,7 +684,7 @@ class TestCase(object): deemed to have suffered an error, exactly as for an unexpected exception.
If called with callableObj omitted or None, will return a[](#l1.51)
If called with callableObj omitted, will return a[](#l1.52) context object used like this::[](#l1.53)
with self.assertRaises(SomeException): @@ -703,7 +705,7 @@ class TestCase(object): context = _AssertRaisesContext(excClass, self, callableObj) return context.handle('assertRaises', callableObj, args, kwargs)
- def assertWarns(self, expected_warning, callable_obj=_sentinel, *args, **kwargs): """Fail unless a warning of class warnClass is triggered by callable_obj when invoked with arguments args and keyword arguments kwargs. If a different type of warning is
@@ -711,7 +713,7 @@ class TestCase(object): warning filtering rules in effect, it might be silenced, printed out, or raised as an exception.
If called with callable_obj omitted or None, will return a[](#l1.69)
If called with callable_obj omitted, will return a[](#l1.70) context object used like this::[](#l1.71)
with self.assertWarns(SomeWarning): @@ -1219,7 +1221,7 @@ class TestCase(object): self.fail(self._formatMessage(msg, standardMsg)) def assertRaisesRegex(self, expected_exception, expected_regex,
callable_obj=None, *args, **kwargs):[](#l1.78)
callable_obj=_sentinel, *args, **kwargs):[](#l1.79) """Asserts that the message in a raised exception matches a regex.[](#l1.80)
Args: @@ -1238,7 +1240,7 @@ class TestCase(object): return context.handle('assertRaisesRegex', callable_obj, args, kwargs) def assertWarnsRegex(self, expected_warning, expected_regex,
callable_obj=None, *args, **kwargs):[](#l1.87)
callable_obj=_sentinel, *args, **kwargs):[](#l1.88) """Asserts that the message in a triggered warning matches a regexp.[](#l1.89) Basic functioning is similar to assertWarns() with the addition[](#l1.90) that only warnings whose messages also match the regular expression[](#l1.91)
--- a/Lib/unittest/test/test_case.py +++ b/Lib/unittest/test/test_case.py @@ -1134,6 +1134,50 @@ test case self.assertRaises(self.failureException, self.assertRegex, 'saaas', r'aaaa')
- def testAssertRaisesCallable(self):
class ExceptionMock(Exception):[](#l2.8)
pass[](#l2.9)
def Stub():[](#l2.10)
raise ExceptionMock('We expect')[](#l2.11)
self.assertRaises(ExceptionMock, Stub)[](#l2.12)
# A tuple of exception classes is accepted[](#l2.13)
self.assertRaises((ValueError, ExceptionMock), Stub)[](#l2.14)
# *args and **kwargs also work[](#l2.15)
self.assertRaises(ValueError, int, '19', base=8)[](#l2.16)
# Failure when no exception is raised[](#l2.17)
with self.assertRaises(self.failureException):[](#l2.18)
self.assertRaises(ExceptionMock, lambda: 0)[](#l2.19)
# Failure when the function is None[](#l2.20)
with self.assertRaises(TypeError):[](#l2.21)
self.assertRaises(ExceptionMock, None)[](#l2.22)
# Failure when another exception is raised[](#l2.23)
with self.assertRaises(ExceptionMock):[](#l2.24)
self.assertRaises(ValueError, Stub)[](#l2.25)
- def testAssertRaisesContext(self):
class ExceptionMock(Exception):[](#l2.28)
pass[](#l2.29)
def Stub():[](#l2.30)
raise ExceptionMock('We expect')[](#l2.31)
with self.assertRaises(ExceptionMock):[](#l2.32)
Stub()[](#l2.33)
# A tuple of exception classes is accepted[](#l2.34)
with self.assertRaises((ValueError, ExceptionMock)) as cm:[](#l2.35)
Stub()[](#l2.36)
# The context manager exposes caught exception[](#l2.37)
self.assertIsInstance(cm.exception, ExceptionMock)[](#l2.38)
self.assertEqual(cm.exception.args[0], 'We expect')[](#l2.39)
# *args and **kwargs also work[](#l2.40)
with self.assertRaises(ValueError):[](#l2.41)
int('19', base=8)[](#l2.42)
# Failure when no exception is raised[](#l2.43)
with self.assertRaises(self.failureException):[](#l2.44)
with self.assertRaises(ExceptionMock):[](#l2.45)
pass[](#l2.46)
# Failure when another exception is raised[](#l2.47)
with self.assertRaises(ExceptionMock):[](#l2.48)
self.assertRaises(ValueError, Stub)[](#l2.49)
+ def testAssertRaisesRegex(self): class ExceptionMock(Exception): pass @@ -1143,6 +1187,8 @@ test case self.assertRaisesRegex(ExceptionMock, re.compile('expect$'), Stub) self.assertRaisesRegex(ExceptionMock, 'expect$', Stub)
with self.assertRaises(TypeError):[](#l2.58)
self.assertRaisesRegex(ExceptionMock, 'expect$', None)[](#l2.59)
def testAssertNotRaisesRegex(self): self.assertRaisesRegex( @@ -1210,6 +1256,9 @@ test case # Failure when no warning is triggered with self.assertRaises(self.failureException): self.assertWarns(RuntimeWarning, lambda: 0)
# Failure when the function is None[](#l2.67)
with self.assertRaises(TypeError):[](#l2.68)
self.assertWarns(RuntimeWarning, None)[](#l2.69) # Failure when another warning is triggered[](#l2.70) with warnings.catch_warnings():[](#l2.71) # Force default filter (in case tests are run with -We)[](#l2.72)
@@ -1271,6 +1320,9 @@ test case with self.assertRaises(self.failureException): self.assertWarnsRegex(RuntimeWarning, "o+", lambda: 0)
# Failure when the function is None[](#l2.77)
with self.assertRaises(TypeError):[](#l2.78)
self.assertWarnsRegex(RuntimeWarning, "o+", None)[](#l2.79) # Failure when another warning is triggered[](#l2.80) with warnings.catch_warnings():[](#l2.81) # Force default filter (in case tests are run with -We)[](#l2.82)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -42,6 +42,9 @@ Core and Builtins Library ------- +- Issue #24134: assertRaises(), assertRaisesRegex(), assertWarns() and