Skip to content

Commit caa08eb

Browse files
committed
Improve quoting in raises match failure message
1 parent b08ae44 commit caa08eb

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

changelog/5479.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve quoting in ``raises`` match failure message.

src/_pytest/_code/code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ def match(self, regexp):
544544
"""
545545
__tracebackhide__ = True
546546
if not re.search(regexp, str(self.value)):
547-
assert 0, "Pattern '{!s}' not found in '{!s}'".format(regexp, self.value)
547+
assert 0, "Pattern {!r} not found in {!r}".format(regexp, str(self.value))
548548
return True
549549

550550

testing/python/raises.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,20 @@ def test_raises_match(self):
220220
int("asdf")
221221

222222
msg = "with base 16"
223-
expr = r"Pattern '{}' not found in 'invalid literal for int\(\) with base 10: 'asdf''".format(
223+
expr = r"Pattern '{}' not found in \"invalid literal for int\(\) with base 10: 'asdf'\"".format(
224224
msg
225225
)
226226
with pytest.raises(AssertionError, match=expr):
227227
with pytest.raises(ValueError, match=msg):
228228
int("asdf", base=10)
229229

230+
def test_match_failure_string_quoting(self):
231+
with pytest.raises(AssertionError) as excinfo:
232+
with pytest.raises(AssertionError, match="'foo"):
233+
raise AssertionError("'bar")
234+
msg, = excinfo.value.args
235+
assert msg == 'Pattern "\'foo" not found in "\'bar"'
236+
230237
def test_raises_match_wrong_type(self):
231238
"""Raising an exception with the wrong type and match= given.
232239

0 commit comments

Comments
 (0)