ID EN
Unittest

assertWarns

Python 3.11

Test that a warning is triggered when callable is called with any positional or keyword arguments that are also passed to assertWarns(). The test passes if warning is triggered and fails if it isn’t. Any exception is an error. To catch any of a group of warnings, a tuple containing the warning classes may be passed as warnings.

Syntax

PYTHON
assertWarns(warning, callable, *args, **kwds)

Examples

Example 1
PYTHON
with self.assertWarns(SomeWarning):
    do_something()
Example 2
PYTHON
with self.assertWarns(SomeWarning) as cm:
    do_something()

self.assertIn('myfile.py', cm.filename)
self.assertEqual(320, cm.lineno)

See Also

assertWarns() assertWarns()