From caa08ebd4536bbabfb0e1edd3d438be911cfab51 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 4 Jul 2019 05:55:26 -0700 Subject: [PATCH] Improve quoting in raises match failure message --- changelog/5479.bugfix.rst | 1 + src/_pytest/_code/code.py | 2 +- testing/python/raises.py | 9 ++++++++- 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 changelog/5479.bugfix.rst diff --git a/changelog/5479.bugfix.rst b/changelog/5479.bugfix.rst new file mode 100644 index 00000000000..d1dd3b0f890 --- /dev/null +++ b/changelog/5479.bugfix.rst @@ -0,0 +1 @@ +Improve quoting in ``raises`` match failure message. diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index b0b4d653119..aa4dcffceb3 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -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 diff --git a/testing/python/raises.py b/testing/python/raises.py index c9ede412ae9..ed3a5cd37c7 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -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.