assertRaisesRegexp在Python2中使用unicode

assertRaisesRegexp在Python2中使用unicode,python,unit-testing,unicode,Python,Unit Testing,Unicode,嗨,我注意到assertRaisesRegexp在Python2.7上不能与unicode一起工作。 我正在尝试运行以下代码 import unittest def raise_exception(): raise Exception(u'\u4e2d\u6587') class TestUnicode(unittest.TestCase): def test_test1(self): with self.assertRaisesRegexp(Excep

嗨,我注意到assertRaisesRegexp在Python2.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,这个函数在Python3上运行良好,没有unicode问题。
关于如何使其在Python2中工作,有什么建议吗?

我在这里遇到了同样的问题,不幸的是我也不能解决它,但我有一个工作要做,在我的情况下,我将我的加薪例外改为:


引发异常(u'\u4e2d\u6587'.encode('utf8'))


这对我来说很有用…

如果显式传入regexp对象,效果似乎更好:

with self.assertRaisesRegexp(Exception, re.compile(u'\u4e2d\u6587')):

assertRaisesRegexp的文档建议您只需传入一个字符串,只要该字符串可以用作regexp,但至少对于我正在使用的python版本–2.7.8–而言,它似乎已损坏。

还有另一种方法,即make
str(exc_值)
工作:

class UnicodeMsgException(Exception):
    def __str__(self):
        return unicode(self).encode('utf-8')
    def __unicode__(self):
        return self.message

您可以编写支持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

您也可以使用monkeypatch
unittest2.case.str
这样的方法,而不是像mtoloo建议的那样重写
assertRaisesRegex

# 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