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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 6 deletions

View File

@ -164,6 +164,7 @@ Kyle Altendorf
Lawrence Mitchell Lawrence Mitchell
Lee Kamentsky Lee Kamentsky
Lev Maximov Lev Maximov
Lewis Cowles
Llandy Riveron Del Risco Llandy Riveron Del Risco
Loic Esteve Loic Esteve
Lukas Bednar Lukas Bednar

View File

@ -0,0 +1 @@
The :func:`pytest.raises` function has a clearer error message when ``match`` equals the obtained string but is not a regex match. In this case it is suggested to escape the regex.

View File

@ -609,9 +609,10 @@ class ExceptionInfo(Generic[_E]):
If it matches `True` is returned, otherwise an `AssertionError` is raised. If it matches `True` is returned, otherwise an `AssertionError` is raised.
""" """
__tracebackhide__ = True __tracebackhide__ = True
assert re.search( msg = "Regex pattern {!r} does not match {!r}."
regexp, str(self.value) if regexp == str(self.value):
), "Pattern {!r} does not match {!r}".format(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 to allow for "assert excinfo.match()".
return True return True

View File

@ -423,7 +423,7 @@ def test_match_raises_error(testdir):
result = testdir.runpytest() result = testdir.runpytest()
assert result.ret != 0 assert result.ret != 0
exc_msg = "Pattern '[[]123[]]+' does not match 'division by zero'" exc_msg = "Regex pattern '[[]123[]]+' does not match 'division by zero'."
result.stdout.fnmatch_lines(["E * AssertionError: {}".format(exc_msg)]) result.stdout.fnmatch_lines(["E * AssertionError: {}".format(exc_msg)])
result.stdout.no_fnmatch_line("*__tracebackhide__ = True*") result.stdout.no_fnmatch_line("*__tracebackhide__ = True*")

View File

@ -197,7 +197,7 @@ class TestRaises:
int("asdf") int("asdf")
msg = "with base 16" msg = "with base 16"
expr = "Pattern {!r} does not match \"invalid literal for int() with base 10: 'asdf'\"".format( expr = "Regex pattern {!r} does not match \"invalid literal for int() with base 10: 'asdf'\".".format(
msg msg
) )
with pytest.raises(AssertionError, match=re.escape(expr)): with pytest.raises(AssertionError, match=re.escape(expr)):
@ -223,7 +223,19 @@ class TestRaises:
with pytest.raises(AssertionError, match="'foo"): with pytest.raises(AssertionError, match="'foo"):
raise AssertionError("'bar") raise AssertionError("'bar")
(msg,) = excinfo.value.args (msg,) = excinfo.value.args
assert msg == 'Pattern "\'foo" does not match "\'bar"' assert msg == 'Regex pattern "\'foo" does not match "\'bar".'
def test_match_failure_exact_string_message(self):
message = "Oh here is a message with (42) numbers in parameters"
with pytest.raises(AssertionError) as excinfo:
with pytest.raises(AssertionError, match=message):
raise AssertionError(message)
(msg,) = excinfo.value.args
assert msg == (
"Regex pattern 'Oh here is a message with (42) numbers in "
"parameters' does not match 'Oh here is a message with (42) "
"numbers in parameters'. Did you mean to `re.escape()` the regex?"
)
def test_raises_match_wrong_type(self): def test_raises_match_wrong_type(self):
"""Raising an exception with the wrong type and match= given. """Raising an exception with the wrong type and match= given.