Created
March 11, 2013 03:07
-
-
Save jerrykan/5131643 to your computer and use it in GitHub Desktop.
Test exceptions are raised and the exception message is correct.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
def raise_exception(yup=True): | |
if yup: | |
raise ValueError('Yup, exception raised.') | |
class ExceptionMessageTestCase(unittest.TestCase): | |
def assertRaisesMessage(self, exception, msg, func, *args, **kwargs): | |
try: | |
func(*args, **kwargs) | |
self.fail() | |
except exception as e: | |
self.assertEqual(str(e), msg) | |
class BasicExceptionTest(unittest.TestCase): | |
def test_simple(self): | |
self.assertRaises(ValueError, raise_exception, True) | |
def test_message(self): | |
try: | |
raise_exception(True) | |
self.fail() | |
except ValueError as e: | |
self.assertEqual(str(e), 'Yup, exception raised.') | |
class MessageExceptionTest(ExceptionMessageTestCase): | |
def test_simple(self): | |
self.assertRaises(ValueError, raise_exception, True) | |
def test_message(self): | |
self.assertRaisesMessage(ValueError, 'Yup, exception raised.', | |
raise_exception, True) | |
if __name__ == '__main__': | |
unittest.main(verbosity=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment