Fail unless an exception of class expected_exception is raised by the callable when invoked with specified positional and keyword arguments. If a different type of exception is raised, it will not be caught, and the test case will be deemed to have suf
(self, expected_exception, *args, **kwargs)
| 736 | return '%s : %s' % (safe_repr(standardMsg), safe_repr(msg)) |
| 737 | |
| 738 | def assertRaises(self, expected_exception, *args, **kwargs): |
| 739 | """Fail unless an exception of class expected_exception is raised |
| 740 | by the callable when invoked with specified positional and |
| 741 | keyword arguments. If a different type of exception is |
| 742 | raised, it will not be caught, and the test case will be |
| 743 | deemed to have suffered an error, exactly as for an |
| 744 | unexpected exception. |
| 745 | |
| 746 | If called with the callable and arguments omitted, will return a |
| 747 | context object used like this:: |
| 748 | |
| 749 | with self.assertRaises(SomeException): |
| 750 | do_something() |
| 751 | |
| 752 | An optional keyword argument 'msg' can be provided when assertRaises |
| 753 | is used as a context object. |
| 754 | |
| 755 | The context manager keeps a reference to the exception as |
| 756 | the 'exception' attribute. This allows you to inspect the |
| 757 | exception after the assertion:: |
| 758 | |
| 759 | with self.assertRaises(SomeException) as cm: |
| 760 | do_something() |
| 761 | the_exception = cm.exception |
| 762 | self.assertEqual(the_exception.error_code, 3) |
| 763 | """ |
| 764 | context = _AssertRaisesContext(expected_exception, self) |
| 765 | try: |
| 766 | return context.handle('assertRaises', args, kwargs) |
| 767 | finally: |
| 768 | # bpo-23890: manually break a reference cycle |
| 769 | context = None |
| 770 | |
| 771 | def assertWarns(self, expected_warning, *args, **kwargs): |
| 772 | """Fail unless a warning of class warnClass is triggered |