ID EN
Unittest

assertWarnsRegex

Python 3.11

Like assertWarns() but also tests that regex matches on the message of the triggered warning. regex may be a regular expression object or a string containing a regular expression suitable for use by re.search(). Example:

Syntax

PYTHON
assertWarnsRegex(warning, regex, callable, *args, **kwds)

Examples

Example 1
PYTHON
self.assertWarnsRegex(DeprecationWarning,
                      r'legacy_function\(\) is deprecated',
                      legacy_function, 'XYZ')
Example 2
PYTHON
with self.assertWarnsRegex(RuntimeWarning, 'unsafe frobnicating'):
    frobnicate('/etc/passwd')

See Also

assertWarns() re.search()