Assert that the expected exception is raised when calling a function, and that the right error message is included with that exception. Arguments: func -- the function to call args -- positional arguments to `func` kwargs -- keyword arg
(self,
func,
args,
kwargs,
expected_exception,
expected_message)
| 78 | return (options, positional_args) |
| 79 | |
| 80 | def assertRaises(self, |
| 81 | func, |
| 82 | args, |
| 83 | kwargs, |
| 84 | expected_exception, |
| 85 | expected_message): |
| 86 | """ |
| 87 | Assert that the expected exception is raised when calling a |
| 88 | function, and that the right error message is included with |
| 89 | that exception. |
| 90 | |
| 91 | Arguments: |
| 92 | func -- the function to call |
| 93 | args -- positional arguments to `func` |
| 94 | kwargs -- keyword arguments to `func` |
| 95 | expected_exception -- exception that should be raised |
| 96 | expected_message -- expected exception message (or pattern |
| 97 | if a compiled regex object) |
| 98 | |
| 99 | Returns the exception raised for further testing. |
| 100 | """ |
| 101 | if args is None: |
| 102 | args = () |
| 103 | if kwargs is None: |
| 104 | kwargs = {} |
| 105 | |
| 106 | try: |
| 107 | func(*args, **kwargs) |
| 108 | except expected_exception as err: |
| 109 | actual_message = str(err) |
| 110 | if isinstance(expected_message, re.Pattern): |
| 111 | self.assertTrue(expected_message.search(actual_message), |
| 112 | """\ |
| 113 | expected exception message pattern: |
| 114 | /%s/ |
| 115 | actual exception message: |
| 116 | '''%s''' |
| 117 | """ % (expected_message.pattern, actual_message)) |
| 118 | else: |
| 119 | self.assertEqual(actual_message, |
| 120 | expected_message, |
| 121 | """\ |
| 122 | expected exception message: |
| 123 | '''%s''' |
| 124 | actual exception message: |
| 125 | '''%s''' |
| 126 | """ % (expected_message, actual_message)) |
| 127 | |
| 128 | return err |
| 129 | else: |
| 130 | self.fail("""expected exception %(expected_exception)s not raised |
| 131 | called %(func)r |
| 132 | with args %(args)r |
| 133 | and kwargs %(kwargs)r |
| 134 | """ % locals ()) |
| 135 | |
| 136 | |
| 137 | # -- Assertions used in more than one class -------------------- |
no test coverage detected