assert_raises(exception_class, callable, *args, **kwargs) assert_raises(exception_class) Fail unless an exception of class exception_class is thrown by callable when invoked with arguments args and keyword arguments kwargs. If a different type of exception is thrown, it wil
(*args, **kwargs)
| 1267 | |
| 1268 | |
| 1269 | def assert_raises(*args, **kwargs): |
| 1270 | """ |
| 1271 | assert_raises(exception_class, callable, *args, **kwargs) |
| 1272 | assert_raises(exception_class) |
| 1273 | |
| 1274 | Fail unless an exception of class exception_class is thrown |
| 1275 | by callable when invoked with arguments args and keyword |
| 1276 | arguments kwargs. If a different type of exception is |
| 1277 | thrown, it will not be caught, and the test case will be |
| 1278 | deemed to have suffered an error, exactly as for an |
| 1279 | unexpected exception. |
| 1280 | |
| 1281 | Alternatively, `assert_raises` can be used as a context manager: |
| 1282 | |
| 1283 | >>> from numpy.testing import assert_raises |
| 1284 | >>> with assert_raises(ZeroDivisionError): |
| 1285 | ... 1 / 0 |
| 1286 | |
| 1287 | is equivalent to |
| 1288 | |
| 1289 | >>> def div(x, y): |
| 1290 | ... return x / y |
| 1291 | >>> assert_raises(ZeroDivisionError, div, 1, 0) |
| 1292 | |
| 1293 | """ |
| 1294 | __tracebackhide__ = True # Hide traceback for py.test |
| 1295 | return _d.assertRaises(*args, **kwargs) |
| 1296 | |
| 1297 | |
| 1298 | def assert_raises_regex(exception_class, expected_regexp, *args, **kwargs): |
no outgoing calls