MCPcopy Index your code
hub / github.com/python/cpython / assertWarns

Method assertWarns

Lib/unittest/case.py:816–845  ·  view source on GitHub ↗

Fail unless a warning of class warnClass is triggered by the callable when invoked with specified positional and keyword arguments. If a different type of warning is triggered, it will not be handled: depending on the other warning filtering rules in effe

(self, expected_warning, *args, **kwargs)

Source from the content-addressed store, hash-verified

814 context = None
815
816 def assertWarns(self, expected_warning, *args, **kwargs):
817 """Fail unless a warning of class warnClass is triggered
818 by the callable when invoked with specified positional and
819 keyword arguments. If a different type of warning is
820 triggered, it will not be handled: depending on the other
821 warning filtering rules in effect, it might be silenced, printed
822 out, or raised as an exception.
823
824 If called with the callable and arguments omitted, will return a
825 context object used like this::
826
827 with self.assertWarns(SomeWarning):
828 do_something()
829
830 An optional keyword argument 'msg' can be provided when assertWarns
831 is used as a context object.
832
833 The context manager keeps a reference to the first matching
834 warning as the 'warning' attribute; similarly, the 'filename'
835 and 'lineno' attributes give you information about the line
836 of Python code from which the warning was triggered.
837 This allows you to inspect the warning after the assertion::
838
839 with self.assertWarns(SomeWarning) as cm:
840 do_something()
841 the_warning = cm.warning
842 self.assertEqual(the_warning.some_attribute, 147)
843 """
844 context = _AssertWarnsContext(expected_warning, self)
845 return context.handle('assertWarns', args, kwargs)
846
847 def _assertNotWarns(self, expected_warning, *args, **kwargs):
848 """The opposite of assertWarns. Private due to low demand."""

Calls 2

_AssertWarnsContextClass · 0.85
handleMethod · 0.45