Skip to content

Commit fd0010e

Browse files
Merge pull request #1439 from pytest-dev/fix-1178
Support pytest.fail with non-ascii characters Fixes #1178
2 parents 9b51536 + 5d47038 commit fd0010e

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
*
77

8+
* Fix (`#1178 <https://github.com/pytest-dev/pytest/issues/1178>`_):
9+
``pytest.fail`` with non-ascii characters raises an internal pytest error.
10+
Thanks `@nicoddemus`_ for the PR.
11+
812
* Fix (`#469`_): junit parses report.nodeid incorrectly, when params IDs
913
contain ``::``. Thanks `@tomviner`_ for the PR (`#1431`_).
1014

_pytest/python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ def _prunetraceback(self, excinfo):
740740
def _repr_failure_py(self, excinfo, style="long"):
741741
if excinfo.errisinstance(pytest.fail.Exception):
742742
if not excinfo.value.pytrace:
743-
return str(excinfo.value)
743+
return py._builtin._totext(excinfo.value)
744744
return super(FunctionMixin, self)._repr_failure_py(excinfo,
745745
style=style)
746746

_pytest/runner.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,10 @@ def __init__(self, msg=None, pytrace=True):
435435

436436
def __repr__(self):
437437
if self.msg:
438-
return str(self.msg)
438+
val = self.msg
439+
if isinstance(val, bytes):
440+
val = py._builtin._totext(val, errors='replace')
441+
return val
439442
return "<%s instance>" %(self.__class__.__name__,)
440443
__str__ = __repr__
441444

testing/test_runner.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: utf-8 -*-
12
from __future__ import with_statement
23

34
import _pytest._code
@@ -439,6 +440,27 @@ def teardown_function(function):
439440
assert 'def teardown_function' not in result.stdout.str()
440441

441442

443+
@pytest.mark.parametrize('str_prefix', ['u', ''])
444+
def test_pytest_fail_notrace_non_ascii(testdir, str_prefix):
445+
"""Fix pytest.fail with pytrace=False with non-ascii characters (#1178).
446+
447+
This tests with native and unicode strings containing non-ascii chars.
448+
"""
449+
testdir.makepyfile(u"""
450+
# coding: utf-8
451+
import pytest
452+
453+
def test_hello():
454+
pytest.fail(%s'oh oh: ☺', pytrace=False)
455+
""" % str_prefix)
456+
result = testdir.runpytest()
457+
if sys.version_info[0] >= 3:
458+
result.stdout.fnmatch_lines(['*test_hello*', "oh oh: ☺"])
459+
else:
460+
result.stdout.fnmatch_lines(['*test_hello*', "oh oh: *"])
461+
assert 'def test_hello' not in result.stdout.str()
462+
463+
442464
def test_pytest_no_tests_collected_exit_status(testdir):
443465
result = testdir.runpytest()
444466
result.stdout.fnmatch_lines('*collected 0 items*')

0 commit comments

Comments
 (0)