Fix pytest.raises handling of unicode exceptions in Python 2 (#5479)

Fix pytest.raises handling of unicode exceptions in Python 2
This commit is contained in:
Bruno Oliveira 2019-07-04 10:25:56 -03:00 committed by GitHub
commit 46a0888352
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 3 deletions

View File

@ -0,0 +1 @@
Fix encode error when using unicode strings in exceptions with ``pytest.raises``.

View File

@ -572,8 +572,13 @@ class ExceptionInfo(object):
raised.
"""
__tracebackhide__ = True
if not re.search(regexp, str(self.value)):
assert 0, "Pattern '{!s}' not found in '{!s}'".format(regexp, self.value)
value = (
text_type(self.value) if isinstance(regexp, text_type) else str(self.value)
)
if not re.search(regexp, value):
raise AssertionError(
u"Pattern {!r} not found in {!r}".format(regexp, value)
)
return True

View File

@ -4,6 +4,7 @@ import sys
import six
import pytest
from _pytest.compat import dummy_context_manager
from _pytest.outcomes import Failed
from _pytest.warning_types import PytestDeprecationWarning
@ -220,7 +221,7 @@ class TestRaises(object):
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):
@ -278,3 +279,47 @@ class TestRaises(object):
with pytest.raises(CrappyClass()):
pass
assert "via __class__" in excinfo.value.args[0]
class TestUnicodeHandling:
"""Test various combinations of bytes and unicode with pytest.raises (#5478)
https://github.com/pytest-dev/pytest/pull/5479#discussion_r298852433
"""
success = dummy_context_manager
py2_only = pytest.mark.skipif(
six.PY3, reason="bytes in raises only supported in Python 2"
)
@pytest.mark.parametrize(
"message, match, expectation",
[
(u"\u2603", u"\u2603", success()),
(u"\u2603", u"\u2603foo", pytest.raises(AssertionError)),
pytest.param(b"hello", b"hello", success(), marks=py2_only),
pytest.param(
b"hello", b"world", pytest.raises(AssertionError), marks=py2_only
),
pytest.param(u"hello", b"hello", success(), marks=py2_only),
pytest.param(
u"hello", b"world", pytest.raises(AssertionError), marks=py2_only
),
pytest.param(
u"😊".encode("UTF-8"),
b"world",
pytest.raises(AssertionError),
marks=py2_only,
),
pytest.param(
u"world",
u"😊".encode("UTF-8"),
pytest.raises(AssertionError),
marks=py2_only,
),
],
)
def test_handling(self, message, match, expectation):
with expectation:
with pytest.raises(RuntimeError, match=match):
raise RuntimeError(message)