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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user