| 1 |
# -*- coding: UTF-8 -*- |
| 2 |
""" |
| 3 |
SIMILAR: #453 |
| 4 |
NOTE: traceback2 (backport for Python2) solves the problem. |
| 5 |
|
| 6 |
Either Exception text (as summary) or traceback python line shows |
| 7 |
special characters correctly. |
| 8 |
|
| 9 |
.. code-block:: python |
| 10 |
|
| 11 |
# -*- coding=utf-8 -*- |
| 12 |
from behave import * |
| 13 |
from hamcrest.core import assert_that, equal_to |
| 14 |
|
| 15 |
@step("Russian text") |
| 16 |
def foo(stop): |
| 17 |
assert_that(False, equal_to(True), u"Всё очень плохо") # cyrillic |
| 18 |
|
| 19 |
And I also have UTF-8 as my console charset. Running this code leads to |
| 20 |
"Assertion Failed: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)" error. |
| 21 |
|
| 22 |
That is becase behave.textutil.text returns six.text_type(e) where 'e' is exception (https://github.com/behave/behave/blob/master/behave/textutil.py#L83). |
| 23 |
|
| 24 |
Changing line 83 to six.text_type(value) solves this issue. |
| 25 |
""" |
| 26 |
|
| 27 |
from __future__ import print_function |
| 28 |
from behave.textutil import text |
| 29 |
from hamcrest.core import assert_that, equal_to |
| 30 |
from hamcrest.library import contains_string |
| 31 |
import six |
| 32 |
import pytest |
| 33 |
if six.PY2: |
| 34 |
import traceback2 as traceback |
| 35 |
else: |
| 36 |
import traceback |
| 37 |
|
| 38 |
|
| 39 |
def foo(): |
| 40 |
assert_that(False, equal_to(True), u"Всё очень плохо") # cyrillic |
| 41 |
|
| 42 |
@pytest.mark.parametrize("encoding", [None, "UTF-8", "unicode_escape"]) |
| 43 |
def test_issue(encoding): |
| 44 |
expected = u"Всё очень плохо" |
| 45 |
try: |
| 46 |
foo() |
| 47 |
except Exception as e: |
| 48 |
text2 = traceback.format_exc() |
| 49 |
|
| 50 |
text3 = text(text2, encoding) |
| 51 |
print(u"EXCEPTION-TEXT: %s" % text3) |
| 52 |
print(u"text2: "+ text2) |
| 53 |
assert_that(text3, contains_string(u"AssertionError: Всё очень плохо")) |
| 54 |
|
| 55 |
|