您好我注意到assertRaisesRegexp不能在Python 2.7上使用unicode。 我正在尝试运行以下代码
import unittest
def raise_exception():
raise Exception(u'\u4e2d\u6587')
class TestUnicode(unittest.TestCase):
def test_test1(self):
with self.assertRaisesRegexp(Exception, u'\u4e2d\u6587'):
raise_exception()
if __name__ == '__main__':
unittest.main()
但得到以下错误
Traceback (most recent call last):
File "C:\ZChenCode\unicode.py", line 27, in test_test1
raise_exception()
File "C:\Python27\ArcGIS10.3\Lib\unittest\case.py", line 127, in __exit__
if not expected_regexp.search(str(exc_value)):
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
看起来Python标准库正在尝试将unicode字符串转换为导致错误的str类型。 如果我使用assertRaiseRegx,没有unicode问题,这个函数在Python3上运行良好。 关于如何使它在Python2中工作的任何建议?
答案 0 :(得分:2)
我在这里遇到了同样的问题,不幸的是我也无法解决这个问题,但我有一个工作方面,在我的情况下我改变了我的加薪例外:
raise Exception(u'\u4e2d\u6587'.encode('utf8'))
这对我有用......
答案 1 :(得分:1)
还有另一种方法,即使str(exc_value)
工作:
class UnicodeMsgException(Exception):
def __str__(self):
return unicode(self).encode('utf-8')
def __unicode__(self):
return self.message
答案 2 :(得分:0)
如果您明确传入正则表达式对象,它似乎更好用:
with self.assertRaisesRegexp(Exception, re.compile(u'\u4e2d\u6587')):
assertRaisesRegexp的文档建议你可以传入一个字符串,只要它可以用作正则表达式,但至少对于我正在使用的python版本 - 2.7.8 - 似乎已被破坏。
答案 3 :(得分:0)
您可以编写支持Unicode的新断言方法:
def assertRaisesRegexpUnicode(self, expected_exception, expected_regexp, callable_obj=None, *args, **kwargs):
"""Asserts that the message in a raised exception matches a regexp.
Args:
expected_exception: Exception class expected to be raised.
expected_regexp: Regexp (re pattern object or string) expected
to be found in error message.
callable_obj: Function to be called.
args: Extra args.
kwargs: Extra kwargs.
"""
if callable_obj is None:
return _AssertRaisesContext(expected_exception, self, expected_regexp)
try:
callable_obj(*args, **kwargs)
except expected_exception, exc_value:
if isinstance(expected_regexp, basestring):
expected_regexp = re.compile(expected_regexp)
actual = exc_value.message
if not expected_regexp.search(actual):
raise self.failureException(u'"{expected}" does not match "{actual}"'.
format(expected=expected_regexp.pattern, actual=actual))
else:
if hasattr(expected_exception, '__name__'):
excName = expected_exception.__name__
else:
excName = str(expected_exception)
raise self.failureException, "%s not raised" % excName
答案 4 :(得分:0)
不像mtoloo建议的那样重写assertRaisesRegex
,你也可以像这样monkeypatch unittest2.case.str
:
# unittest2's assertRaisesRegex doesn't do unicode comparison.
# Let's monkeypatch the str() function to point to unicode()
# so that it does :)
# For reference, this is where this patch is required:
# https://hg.python.org/unittest2/file/tip/unittest2/case.py#l227
try:
unittest2.case.str = unicode
except Exception:
pass # python 3