|
| 1 | +import unittest |
| 2 | +import sys |
| 3 | + |
| 4 | +if sys.version_info < (2, 7): |
| 5 | + # Stolen from unittest2 |
| 6 | + import re |
| 7 | + |
| 8 | + class _AssertRaisesBaseContext(object): |
| 9 | + |
| 10 | + def __init__(self, expected, test_case, callable_obj=None, |
| 11 | + expected_regex=None): |
| 12 | + self.expected = expected |
| 13 | + self.failureException = test_case.failureException |
| 14 | + if callable_obj is not None: |
| 15 | + try: |
| 16 | + self.obj_name = callable_obj.__name__ |
| 17 | + except AttributeError: |
| 18 | + self.obj_name = str(callable_obj) |
| 19 | + else: |
| 20 | + self.obj_name = None |
| 21 | + if isinstance(expected_regex, basestring): |
| 22 | + expected_regex = re.compile(expected_regex) |
| 23 | + self.expected_regex = expected_regex |
| 24 | + |
| 25 | + class _AssertRaisesContext(_AssertRaisesBaseContext): |
| 26 | + """A context manager used to implement TestCase.assertRaises* methods.""" |
| 27 | + |
| 28 | + def __enter__(self): |
| 29 | + return self |
| 30 | + |
| 31 | + def __exit__(self, exc_type, exc_value, tb): |
| 32 | + if exc_type is None: |
| 33 | + try: |
| 34 | + exc_name = self.expected.__name__ |
| 35 | + except AttributeError: |
| 36 | + exc_name = str(self.expected) |
| 37 | + raise self.failureException( |
| 38 | + "%s not raised" % (exc_name,)) |
| 39 | + if not issubclass(exc_type, self.expected): |
| 40 | + # let unexpected exceptions pass through |
| 41 | + return False |
| 42 | + self.exception = exc_value # store for later retrieval |
| 43 | + if self.expected_regex is None: |
| 44 | + return True |
| 45 | + |
| 46 | + expected_regex = self.expected_regex |
| 47 | + if not expected_regex.search(str(exc_value)): |
| 48 | + raise self.failureException( |
| 49 | + '%r does not match %r' % |
| 50 | + (expected_regex.pattern, str(exc_value))) |
| 51 | + return True |
| 52 | + |
| 53 | + class TestCase(unittest.TestCase): |
| 54 | + def assertRaises(self, excClass, callableObj=None, *args, **kwargs): |
| 55 | + """Fail unless an exception of class excClass is thrown |
| 56 | + by callableObj when invoked with arguments args and keyword |
| 57 | + arguments kwargs. If a different type of exception is |
| 58 | + thrown, it will not be caught, and the test case will be |
| 59 | + deemed to have suffered an error, exactly as for an |
| 60 | + unexpected exception. |
| 61 | +
|
| 62 | + If called with callableObj omitted or None, will return a |
| 63 | + context object used like this:: |
| 64 | +
|
| 65 | + with self.assertRaises(SomeException): |
| 66 | + do_something() |
| 67 | +
|
| 68 | + The context manager keeps a reference to the exception as |
| 69 | + the 'exception' attribute. This allows you to inspect the |
| 70 | + exception after the assertion:: |
| 71 | +
|
| 72 | + with self.assertRaises(SomeException) as cm: |
| 73 | + do_something() |
| 74 | + the_exception = cm.exception |
| 75 | + self.assertEqual(the_exception.error_code, 3) |
| 76 | + """ |
| 77 | + if callableObj is None: |
| 78 | + return _AssertRaisesContext(excClass, self) |
| 79 | + try: |
| 80 | + callableObj(*args, **kwargs) |
| 81 | + except excClass: |
| 82 | + return |
| 83 | + |
| 84 | + if hasattr(excClass, '__name__'): |
| 85 | + excName = excClass.__name__ |
| 86 | + else: |
| 87 | + excName = str(excClass) |
| 88 | + raise self.failureException("%s not raised" % excName) |
| 89 | +else: |
| 90 | + TestCase = unittest.TestCase |
0 commit comments