Skip to content

Commit

Permalink
Improve quoting in raises match failure message (pytest-dev#5553)
Browse files Browse the repository at this point in the history
Improve quoting in raises match failure message
  • Loading branch information
nicoddemus committed Jul 4, 2019
2 parents b08ae44 + caa08eb commit 95824c5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/5479.bugfix.rst
@@ -0,0 +1 @@
Improve quoting in ``raises`` match failure message.
2 changes: 1 addition & 1 deletion src/_pytest/_code/code.py
Expand Up @@ -544,7 +544,7 @@ def match(self, regexp):
"""
__tracebackhide__ = True
if not re.search(regexp, str(self.value)):
assert 0, "Pattern '{!s}' not found in '{!s}'".format(regexp, self.value)
assert 0, "Pattern {!r} not found in {!r}".format(regexp, str(self.value))
return True


Expand Down
9 changes: 8 additions & 1 deletion testing/python/raises.py
Expand Up @@ -220,13 +220,20 @@ def test_raises_match(self):
int("asdf")

msg = "with base 16"
expr = r"Pattern '{}' not found in 'invalid literal for int\(\) with base 10: 'asdf''".format(
expr = r"Pattern '{}' not found in \"invalid literal for int\(\) with base 10: 'asdf'\"".format(
msg
)
with pytest.raises(AssertionError, match=expr):
with pytest.raises(ValueError, match=msg):
int("asdf", base=10)

def test_match_failure_string_quoting(self):
with pytest.raises(AssertionError) as excinfo:
with pytest.raises(AssertionError, match="'foo"):
raise AssertionError("'bar")
msg, = excinfo.value.args
assert msg == 'Pattern "\'foo" not found in "\'bar"'

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

0 comments on commit 95824c5

Please sign in to comment.