summaryrefslogtreecommitdiff
path: root/Lib/unittest/test/test_case.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-05-06 19:14:47 +0300
committerSerhiy Storchaka <storchaka@gmail.com>2015-05-06 19:14:47 +0300
commit24d3b7f9df574b3e3859f20825f2b96730380fc9 (patch)
treeaed800d4cbf935e21a9cbef82d4f1bef03ba2fc5 /Lib/unittest/test/test_case.py
parent8d0f620285d160c454e024c6da0fbf1686264aed (diff)
parente130503c7bc87023d20a353e2ab5081983a40917 (diff)
downloadcpython-git-24d3b7f9df574b3e3859f20825f2b96730380fc9.tar.gz
Issue #24134: assertRaises(), assertRaisesRegex(), assertWarns() and
assertWarnsRegex() checks are not longer successful if the callable is None. Added tests for assertRaises().
Diffstat (limited to 'Lib/unittest/test/test_case.py')
-rw-r--r--Lib/unittest/test/test_case.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py
index 503ad2fb11..75bb0d6014 100644
--- a/Lib/unittest/test/test_case.py
+++ b/Lib/unittest/test/test_case.py
@@ -1132,6 +1132,50 @@ test case
self.assertRaises(self.failureException, self.assertRegex,
'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 testAssertRaisesRegex(self):
class ExceptionMock(Exception):
pass
@@ -1141,6 +1185,8 @@ test case
self.assertRaisesRegex(ExceptionMock, re.compile('expect$'), Stub)
self.assertRaisesRegex(ExceptionMock, 'expect$', Stub)
+ with self.assertRaises(TypeError):
+ self.assertRaisesRegex(ExceptionMock, 'expect$', None)
def testAssertNotRaisesRegex(self):
self.assertRaisesRegex(
@@ -1208,6 +1254,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
+ with self.assertRaises(TypeError):
+ self.assertWarns(RuntimeWarning, None)
# Failure when another warning is triggered
with warnings.catch_warnings():
# Force default filter (in case tests are run with -We)
@@ -1269,6 +1318,9 @@ test case
with self.assertRaises(self.failureException):
self.assertWarnsRegex(RuntimeWarning, "o+",
lambda: 0)
+ # Failure when the function is None
+ with self.assertRaises(TypeError):
+ self.assertWarnsRegex(RuntimeWarning, "o+", None)
# Failure when another warning is triggered
with warnings.catch_warnings():
# Force default filter (in case tests are run with -We)