PyDocs

Nav

Development Tools

TestCase.assertWarns

unittest

method of TestCase · Added in 3.2 (source)
  • TestCase.assertWarns(warning, callable, *args, **kwds)
  • TestCase.assertWarns(warning, *, msg=None)
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 effect, it might be silenced, printed
out, or raised as an exception.

If called with the callable and arguments omitted, will return a
context object used like this::

     with self.assertWarns(SomeWarning):
         do_something()

An optional keyword argument 'msg' can be provided when assertWarns
is used as a context object.

The context manager keeps a reference to the first matching
warning as the 'warning' attribute; similarly, the 'filename'
and 'lineno' attributes give you information about the line
of Python code from which the warning was triggered.
This allows you to inspect the warning after the assertion::

    with self.assertWarns(SomeWarning) as cm:
        do_something()
    the_warning = cm.warning
    self.assertEqual(the_warning.some_attribute, 147)
Changed in 3.3, 3.15
  • 3.3: Added the msg keyword argument when used as a context manager.
  • 3.15: Warnings that do not match the specified category are no longer swallowed. Nested context managers are now supported.
0 Examples
No examples for unittest/TestCase.assertWarns.
See Also
No see-alsos for unittest/TestCase.assertWarns
0 Notes
No notes for TestCase.assertWarns