Skip to content

Commit a886015

Browse files
committed
Test various bytes <=> unicode cases as requested in review
1 parent 09dee29 commit a886015

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/_pytest/_code/code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ def match(self, regexp):
577577
)
578578
if not re.search(regexp, value):
579579
raise AssertionError(
580-
"Pattern '{!s}' not found in '{!s}'".format(regexp, value)
580+
u"Pattern '{}' not found in '{}'".format(regexp, value)
581581
)
582582
return True
583583

testing/python/raises.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import six
55

66
import pytest
7+
from _pytest.compat import dummy_context_manager
78
from _pytest.outcomes import Failed
89
from _pytest.warning_types import PytestDeprecationWarning
910

@@ -279,8 +280,34 @@ def __class__(self):
279280
pass
280281
assert "via __class__" in excinfo.value.args[0]
281282

282-
def test_unicode_message(self):
283-
"""pytest.raises should be able to match unicode messages when using a unicode regex (#5478)
284-
"""
285-
with pytest.raises(AssertionError, match=u"\u2603"):
286-
assert False, u"\u2603"
283+
284+
class TestUnicodeHandling:
285+
"""Test various combinations of bytes and unicode with pytest.raises (#5478)
286+
287+
https://github.com/pytest-dev/pytest/pull/5479#discussion_r298852433
288+
"""
289+
290+
success = dummy_context_manager
291+
py2_only = pytest.mark.skipif(
292+
six.PY3, reason="bytes in raises only supported in Python 2"
293+
)
294+
295+
@pytest.mark.parametrize(
296+
"message, match, expectation",
297+
[
298+
(u"\u2603", u"\u2603", success()),
299+
(u"\u2603", u"\u2603foo", pytest.raises(AssertionError)),
300+
pytest.param(b"hello", b"hello", success(), marks=py2_only),
301+
pytest.param(
302+
b"hello", b"world", pytest.raises(AssertionError), marks=py2_only
303+
),
304+
pytest.param(u"hello", b"hello", success(), marks=py2_only),
305+
pytest.param(
306+
u"hello", b"world", pytest.raises(AssertionError), marks=py2_only
307+
),
308+
],
309+
)
310+
def test_handling(self, message, match, expectation):
311+
with expectation:
312+
with pytest.raises(RuntimeError, match=match):
313+
raise RuntimeError(message)

0 commit comments

Comments
 (0)