Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement: extend pytest.raises to support Exception having __repr__ method and initialized with kwargs #11073

Merged
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Ben Gartner
Ben Webb
Benjamin Peterson
Bernard Pratz
Bo Wu
Bob Ippolito
Brian Dorsey
Brian Larsen
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ def match(self, regexp: Union[str, Pattern[str]]) -> "Literal[True]":
"""
__tracebackhide__ = True
value = str(self.value)
msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n Input: {value!r}"
msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n str(exception): {value!r}"
if regexp == value:
msg += "\n Did you mean to `re.escape()` the regex?"
assert re.search(regexp, value), msg
Expand Down
2 changes: 1 addition & 1 deletion testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def test_division_zero():
match = [
r"E .* AssertionError: Regex pattern did not match.",
r"E .* Regex: '\[123\]\+'",
r"E .* Input: 'division by zero'",
r"E .* str\(exception\): 'division by zero'",
]
result.stdout.re_match_lines(match)
result.stdout.no_fnmatch_line("*__tracebackhide__ = True*")
Expand Down
9 changes: 6 additions & 3 deletions testing/python/raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def test_raises_match(self) -> None:
expr = (
"Regex pattern did not match.\n"
f" Regex: {msg!r}\n"
" Input: \"invalid literal for int() with base 10: 'asdf'\""
" str(exception): \"invalid literal for int() with base 10: 'asdf'\""
)
with pytest.raises(AssertionError, match="(?m)" + re.escape(expr)):
with pytest.raises(ValueError, match=msg):
Expand All @@ -221,7 +221,10 @@ def test_match_failure_string_quoting(self):
with pytest.raises(AssertionError, match="'foo"):
raise AssertionError("'bar")
(msg,) = excinfo.value.args
assert msg == '''Regex pattern did not match.\n Regex: "'foo"\n Input: "'bar"'''
assert (
msg
== '''Regex pattern did not match.\n Regex: "'foo"\n str(exception): "'bar"'''
)

def test_match_failure_exact_string_message(self):
message = "Oh here is a message with (42) numbers in parameters"
Expand All @@ -232,7 +235,7 @@ def test_match_failure_exact_string_message(self):
assert msg == (
"Regex pattern did not match.\n"
" Regex: 'Oh here is a message with (42) numbers in parameters'\n"
" Input: 'Oh here is a message with (42) numbers in parameters'\n"
" str(exception): 'Oh here is a message with (42) numbers in parameters'\n"
" Did you mean to `re.escape()` the regex?"
)

Expand Down