Clearer guidance on pytest.raise(match=...) failure (#7499)

This commit is contained in:
Lewis Cowles
2020-07-15 20:26:47 +01:00
committed by GitHub
parent 9c2c5d9f05
commit 71ab6236a1
5 changed files with 21 additions and 6 deletions

View File

@@ -609,9 +609,10 @@ class ExceptionInfo(Generic[_E]):
If it matches `True` is returned, otherwise an `AssertionError` is raised.
"""
__tracebackhide__ = True
assert re.search(
regexp, str(self.value)
), "Pattern {!r} does not match {!r}".format(regexp, str(self.value))
msg = "Regex pattern {!r} does not match {!r}."
if regexp == str(self.value):
msg += " Did you mean to `re.escape()` the regex?"
assert re.search(regexp, str(self.value)), msg.format(regexp, str(self.value))
# Return True to allow for "assert excinfo.match()".
return True