diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-05-06 16:10:40 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-05-06 16:10:40 (GMT) |
commit | 7f71e04cb510c24be337a22350324dc8a28e9775 (patch) | |
tree | dbeb710a30e361a92f2eeec75de79491c7f8983f /Lib/unittest/test/test_case.py | |
parent | 3234abb9a057beb88faeef96745f8c78772a88c2 (diff) | |
download | cpython-7f71e04cb510c24be337a22350324dc8a28e9775.zip cpython-7f71e04cb510c24be337a22350324dc8a28e9775.tar.gz cpython-7f71e04cb510c24be337a22350324dc8a28e9775.tar.bz2 |
Issue #24134: assertRaises() and assertRaisesRegexp() 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.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py index 4ddf436..4c2d1f9 100644 --- a/Lib/unittest/test/test_case.py +++ b/Lib/unittest/test/test_case.py @@ -954,6 +954,50 @@ test case self.assertRaises(self.failureException, self.assertRegexpMatches, '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): class ExceptionMock(Exception): pass @@ -964,6 +1008,8 @@ test case self.assertRaisesRegexp(ExceptionMock, re.compile('expect$'), Stub) self.assertRaisesRegexp(ExceptionMock, 'expect$', Stub) self.assertRaisesRegexp(ExceptionMock, u'expect$', Stub) + with self.assertRaises(TypeError): + self.assertRaisesRegexp(ExceptionMock, 'expect$', None) def testAssertNotRaisesRegexp(self): self.assertRaisesRegexp( |