From 24468a6f341f5329789746e7840ebc3249e81492 Mon Sep 17 00:00:00 2001 From: pytry Date: Sun, 5 Oct 2014 14:49:15 +0200 Subject: [PATCH 1/2] Added test for ValueError in custom assert message with % sign (issue #604) https://bitbucket.org/hpk42/pytest/issue/604/valueerror-unsupported-format-character-in --HG-- branch : test_for_issue_604 --- testing/test_assertmessage.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 testing/test_assertmessage.py diff --git a/testing/test_assertmessage.py b/testing/test_assertmessage.py new file mode 100644 index 000000000..ca67ff55a --- /dev/null +++ b/testing/test_assertmessage.py @@ -0,0 +1,18 @@ + +def test_assert_message_fail(): + ''' + Check if custom message with % sign do not raise ValueError + Later test can be parametrized with other problematic chars + ''' + + MESSAGE = 'Message with %' + + try: + assert False, MESSAGE + except ValueError, ve: + assert False, 'ValueError was raised with the following message: ' \ + + ve.message + except AssertionError, ae: + assert MESSAGE == ae.message, 'Assertion message: ' + ae.message \ + + ' is different than expected: ' + MESSAGE + \ No newline at end of file From e1aed27c1584a3497ff7d7cb97266ebb8f454fff Mon Sep 17 00:00:00 2001 From: Anatoly Bubenkov Date: Tue, 7 Oct 2014 01:01:21 +0200 Subject: [PATCH 2/2] Escape % character in the assertion message. closes #604 --HG-- branch : test_for_issue_604 --- CHANGELOG | 3 +++ _pytest/assertion/rewrite.py | 2 +- testing/test_assertmessage.py | 18 ------------------ testing/test_assertrewrite.py | 12 ++++++++++++ 4 files changed, 16 insertions(+), 19 deletions(-) delete mode 100644 testing/test_assertmessage.py diff --git a/CHANGELOG b/CHANGELOG index 06f9fca05..d3da98594 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,9 @@ Unreleased - removed outdated japanese docs from source tree. +- Escape % character in the assertion message. + + 2.6.3 ----------- diff --git a/_pytest/assertion/rewrite.py b/_pytest/assertion/rewrite.py index 18df7791e..57edcd487 100644 --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -373,7 +373,7 @@ def _format_assertmsg(obj): t = py.builtin.text else: t = py.builtin.bytes - s = s.replace(t("\n"), t("\n~")) + s = s.replace(t("\n"), t("\n~")).replace(t("%"), t("%%")) if is_repr: s = s.replace(t("\\n"), t("\n~")) return s diff --git a/testing/test_assertmessage.py b/testing/test_assertmessage.py deleted file mode 100644 index ca67ff55a..000000000 --- a/testing/test_assertmessage.py +++ /dev/null @@ -1,18 +0,0 @@ - -def test_assert_message_fail(): - ''' - Check if custom message with % sign do not raise ValueError - Later test can be parametrized with other problematic chars - ''' - - MESSAGE = 'Message with %' - - try: - assert False, MESSAGE - except ValueError, ve: - assert False, 'ValueError was raised with the following message: ' \ - + ve.message - except AssertionError, ae: - assert MESSAGE == ae.message, 'Assertion message: ' + ae.message \ - + ' is different than expected: ' + MESSAGE - \ No newline at end of file diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 9d69b5f49..225608281 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -172,6 +172,18 @@ class TestAssertionRewrite: "*assert 1 == 2*", ]) + def test_assertion_message_escape(self, testdir): + testdir.makepyfile(""" + def test_foo(): + assert 1 == 2, 'To be escaped: %' + """) + result = testdir.runpytest() + assert result.ret == 1 + result.stdout.fnmatch_lines([ + "*AssertionError: To be escaped: %", + "*assert 1 == 2", + ]) + def test_boolop(self): def f(): f = g = False