Skip to content

Commit

Permalink
Change Exception message
Browse files Browse the repository at this point in the history
  • Loading branch information
Lewiscowles1986 committed Jul 14, 2020
1 parent 358150c commit ab13c8b
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -164,6 +164,7 @@ Kyle Altendorf
Lawrence Mitchell
Lee Kamentsky
Lev Maximov
Lewis Cowles
Llandy Riveron Del Risco
Loic Esteve
Lukas Bednar
Expand Down
1 change: 1 addition & 0 deletions changelog/7489.improvement.rst
@@ -0,0 +1 @@
The ``raises()`` method of ``_pytest.python_api`` has a clearer error message.
5 changes: 3 additions & 2 deletions src/_pytest/_code/code.py
Expand Up @@ -609,9 +609,10 @@ def match(self, regexp: "Union[str, Pattern]") -> "Literal[True]":
If it matches `True` is returned, otherwise an `AssertionError` is raised.
"""
__tracebackhide__ = True
assert re.search(
regextest = re.search(regexp, str(self.value))
assert regextest, "Regex pattern r{!r} does not match {!r}".format(
regexp, str(self.value)
), "Pattern {!r} does not match {!r}".format(regexp, str(self.value))
)
# Return True to allow for "assert excinfo.match()".
return True

Expand Down
2 changes: 1 addition & 1 deletion testing/code/test_excinfo.py
Expand Up @@ -423,7 +423,7 @@ def test_division_zero():
result = testdir.runpytest()
assert result.ret != 0

exc_msg = "Pattern '[[]123[]]+' does not match 'division by zero'"
exc_msg = "Regex pattern r'[[]123[]]+' does not match 'division by zero'"
result.stdout.fnmatch_lines(["E * AssertionError: {}".format(exc_msg)])
result.stdout.no_fnmatch_line("*__tracebackhide__ = True*")

Expand Down
4 changes: 2 additions & 2 deletions testing/python/raises.py
Expand Up @@ -197,7 +197,7 @@ def test_raises_match(self) -> None:
int("asdf")

msg = "with base 16"
expr = "Pattern {!r} does not match \"invalid literal for int() with base 10: 'asdf'\"".format(
expr = "Regex pattern r{!r} does not match \"invalid literal for int() with base 10: 'asdf'\"".format(
msg
)
with pytest.raises(AssertionError, match=re.escape(expr)):
Expand All @@ -223,7 +223,7 @@ def test_match_failure_string_quoting(self):
with pytest.raises(AssertionError, match="'foo"):
raise AssertionError("'bar")
(msg,) = excinfo.value.args
assert msg == 'Pattern "\'foo" does not match "\'bar"'
assert msg == 'Regex pattern r"\'foo" does not match "\'bar"'

def test_raises_match_wrong_type(self):
"""Raising an exception with the wrong type and match= given.
Expand Down

0 comments on commit ab13c8b

Please sign in to comment.