From 09dee292ca87424a763bbf0ffeb4bd0e346b76cd Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 30 Jun 2019 10:34:40 -0300 Subject: [PATCH] Use unicode message if regex is also unicode in ExceptionInfo.match --- src/_pytest/_code/code.py | 8 ++++++-- testing/python/raises.py | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index e621f3ee0..e99575e11 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -572,9 +572,13 @@ class ExceptionInfo(object): raised. """ __tracebackhide__ = True - value = safe_str(self.value) + value = ( + text_type(self.value) if isinstance(regexp, text_type) else str(self.value) + ) if not re.search(regexp, value): - assert 0, "Pattern '{!s}' not found in '{!s}'".format(regexp, value) + raise AssertionError( + "Pattern '{!s}' not found in '{!s}'".format(regexp, value) + ) return True diff --git a/testing/python/raises.py b/testing/python/raises.py index db34c6624..ba7a04b8c 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -279,6 +279,8 @@ class TestRaises(object): pass assert "via __class__" in excinfo.value.args[0] - def test_u(self): + def test_unicode_message(self): + """pytest.raises should be able to match unicode messages when using a unicode regex (#5478) + """ with pytest.raises(AssertionError, match=u"\u2603"): assert False, u"\u2603"