Skip to content

Instantly share code, notes, and snippets.

@jerrykan
Created March 11, 2013 03:07
Show Gist options
  • Save jerrykan/5131643 to your computer and use it in GitHub Desktop.
Save jerrykan/5131643 to your computer and use it in GitHub Desktop.
Test exceptions are raised and the exception message is correct.
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