Error messages in BDD are always converted to UTF-8 regardless python version for PY-15258

This commit is contained in:
Ilya.Kazakevich
2015-04-14 22:46:30 +03:00
parent 81fde236dd
commit 47fbe0f2f9
3 changed files with 52 additions and 8 deletions
+45 -3
View File
@@ -8,7 +8,7 @@ You may also need "get_what_to_run_by_env" that gets folder (current or passed a
import os
import time
import abc
import sys
import tcmessages
@@ -139,11 +139,11 @@ class BddRunner(object):
:param name: test name
:type name str
:param message: failure message
:type message str
:type message basestring
:param details: failure details (probably stacktrace)
:type details str
"""
self.tc_messages.testFailed(name, message=message, details=details)
self.tc_messages.testFailed(name, message=VersionAgnosticUtils().to_unicode(message), details=details)
self.__last_test_name = None
def _test_passed(self, name, duration=None):
@@ -229,3 +229,45 @@ class BddRunner(object):
pass
class VersionAgnosticUtils(object):
"""
"six" emulator: this class fabrics appropriate tool to use regardless python version.
Use it to write code that works both on py2 and py3
"""
@staticmethod
def __new__(cls, *more):
"""
Fabrics Py2 or Py3 instance based on py version
"""
real_class = _Py3KUtils if sys.version_info >= (3, 0) else _Py2Utils
return super(cls, real_class).__new__(real_class, *more)
def to_unicode(self, obj):
"""
:param obj: string to convert to unicode
:return: unicode string
"""
raise NotImplementedError()
class _Py2Utils(VersionAgnosticUtils):
"""
Util for Py2
"""
def to_unicode(self, obj):
if isinstance(obj, unicode):
return obj
return unicode(obj.decode("utf-8"))
class _Py3KUtils(VersionAgnosticUtils):
"""
Util for Py3
"""
def to_unicode(self, obj):
return str(obj)
+6 -4
View File
@@ -136,9 +136,10 @@ class _BehaveRunner(_bdd_utils.BddRunner):
:param element feature/suite/step
"""
element.location.file = element.location.filename # To preserve _bdd_utils contract
utils = _bdd_utils.VersionAgnosticUtils()
if isinstance(element, Step):
# Process step
step_name = "{0} {1}".format(element.keyword, element.name)
step_name = u"{0} {1}".format(utils.to_unicode(element.keyword), utils.to_unicode(element.name))
if is_started:
self._test_started(step_name, element.location)
elif element.status == 'passed':
@@ -148,9 +149,10 @@ class _BehaveRunner(_bdd_utils.BddRunner):
trace = traceback.format_exc()
except Exception:
trace = "".join(traceback.format_tb(element.exc_traceback))
if trace in str(element.error_message):
trace = None # No reason to duplicate output (see PY-13647)
self._test_failed(step_name, element.error_message, trace)
error_message = utils.to_unicode(element.error_message)
if "Traceback " in error_message:
error_message = "" # No reason to duplicate output (see PY-13647)
self._test_failed(step_name, error_message, trace)
elif element.status == 'undefined':
self._test_undefined(step_name, element.location)
else:
+1 -1
View File
@@ -103,7 +103,7 @@ class _LettuceRunner(_bdd_utils.BddRunner):
elif step.failed:
reason = step.why
assert isinstance(reason, ReasonToFail), reason
self._test_failed(test_name, message=reason.exception, details=reason.traceback)
self._test_failed(test_name, message=reason.exception.message, details=reason.traceback)
elif step.has_definition:
self._test_skipped(test_name, "In lettuce, we do know the reason", step.described_at)
else: