ID EN
Unittest

assertRaisesRegex

Python 3.11

Like assertRaises() but also tests that regex matches on the string representation of the raised exception. regex may be a regular expression object or a string containing a regular expression suitable for use by re.search(). Examples:

Syntax

PYTHON
assertRaisesRegex(exception, regex, callable, *args, **kwds)

Examples

Example 1
PYTHON
self.assertRaisesRegex(ValueError, "invalid literal for.*XYZ'$",
                       int, 'XYZ')
Example 2
PYTHON
with self.assertRaisesRegex(ValueError, 'literal'):
   int('XYZ')

See Also

assertRaises() re.search() assertRaisesRegex()