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 suffere
(self, expected_exception, *args, **kwargs)
| 781 | return '%s : %s' % (safe_repr(standardMsg), safe_repr(msg)) |
| 782 | |
| 783 | def assertRaises(self, expected_exception, *args, **kwargs): |
| 784 | """Fail unless an exception of class expected_exception is raised |
| 785 | by the callable when invoked with specified positional and |
| 786 | keyword arguments. If a different type of exception is |
| 787 | raised, it will not be caught, and the test case will be |
| 788 | deemed to have suffered an error, exactly as for an |
| 789 | unexpected exception. |
| 790 | |
| 791 | If called with the callable and arguments omitted, will return a |
| 792 | context object used like this:: |
| 793 | |
| 794 | with self.assertRaises(SomeException): |
| 795 | do_something() |
| 796 | |
| 797 | An optional keyword argument 'msg' can be provided when assertRaises |
| 798 | is used as a context object. |
| 799 | |
| 800 | The context manager keeps a reference to the exception as |
| 801 | the 'exception' attribute. This allows you to inspect the |
| 802 | exception after the assertion:: |
| 803 | |
| 804 | with self.assertRaises(SomeException) as cm: |
| 805 | do_something() |
| 806 | the_exception = cm.exception |
| 807 | self.assertEqual(the_exception.error_code, 3) |
| 808 | """ |
| 809 | context = _AssertRaisesContext(expected_exception, self) |
| 810 | try: |
| 811 | return context.handle('assertRaises', args, kwargs) |
| 812 | finally: |
| 813 | # bpo-23890: manually break a reference cycle |
| 814 | context = None |
| 815 | |
| 816 | def assertWarns(self, expected_warning, *args, **kwargs): |
| 817 | """Fail unless a warning of class warnClass is triggered |