Implement ExceptionInfo.match() to match regexp on str(exception)

This implements similar functionality to
unittest.TestCase.assertRegexpMatches()

closes #372
This commit is contained in:
Omar Kohl
2016-04-02 17:24:08 +02:00
parent fed89ef549
commit c578226d43
4 changed files with 58 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
import sys
from inspect import CO_VARARGS, CO_VARKEYWORDS
import re
import py
@@ -427,6 +428,19 @@ class ExceptionInfo(object):
loc = ReprFileLocation(entry.path, entry.lineno + 1, self.exconly())
return unicode(loc)
def match(self, regexp):
"""
Match the regular expression 'regexp' on the string representation of
the exception. If it matches then True is returned (so that it is
possible to write 'assert excinfo.match()'). If it doesn't match an
AssertionError is raised.
"""
__tracebackhide__ = True
if not re.search(regexp, str(self.value)):
assert 0, "Pattern '{0!s}' not found in '{1!s}'".format(
regexp, self.value)
return True
class FormattedExcinfo(object):
""" presenting information about failing Functions and Generators. """