|
4 | 4 | import six
|
5 | 5 |
|
6 | 6 | import pytest
|
| 7 | +from _pytest.compat import dummy_context_manager |
7 | 8 | from _pytest.outcomes import Failed
|
8 | 9 | from _pytest.warning_types import PytestDeprecationWarning
|
9 | 10 |
|
@@ -220,7 +221,7 @@ def test_raises_match(self):
|
220 | 221 | int("asdf")
|
221 | 222 |
|
222 | 223 | msg = "with base 16"
|
223 |
| - expr = r"Pattern '{}' not found in 'invalid literal for int\(\) with base 10: 'asdf''".format( |
| 224 | + expr = r"Pattern '{}' not found in \"invalid literal for int\(\) with base 10: 'asdf'\"".format( |
224 | 225 | msg
|
225 | 226 | )
|
226 | 227 | with pytest.raises(AssertionError, match=expr):
|
@@ -278,3 +279,47 @@ def __class__(self):
|
278 | 279 | with pytest.raises(CrappyClass()):
|
279 | 280 | pass
|
280 | 281 | assert "via __class__" in excinfo.value.args[0]
|
| 282 | + |
| 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 | + pytest.param( |
| 309 | + u"😊".encode("UTF-8"), |
| 310 | + b"world", |
| 311 | + pytest.raises(AssertionError), |
| 312 | + marks=py2_only, |
| 313 | + ), |
| 314 | + pytest.param( |
| 315 | + u"world", |
| 316 | + u"😊".encode("UTF-8"), |
| 317 | + pytest.raises(AssertionError), |
| 318 | + marks=py2_only, |
| 319 | + ), |
| 320 | + ], |
| 321 | + ) |
| 322 | + def test_handling(self, message, match, expectation): |
| 323 | + with expectation: |
| 324 | + with pytest.raises(RuntimeError, match=match): |
| 325 | + raise RuntimeError(message) |
0 commit comments