我在 Windows 中使用 Python 2.7.9。
我有一个 UTF-8 编码的 python 脚本文件,其中包含以下内容:
# coding=utf-8
def test_func():
u"""
>>> test_func()
u'☃'
"""
return u'☃'
当我运行 doctest 时,我遇到了一个奇怪的失败:
Failed example:
test_func()
Expected:
u'\u2603'
Got:
u'\u2603'
无论我是通过通常使用的 IDE (IDEA IntelliJ) 还是从命令行启动文档测试,我都会看到相同的失败输出:
> x:\my_virtualenv\Scripts\python.exe -m doctest -v hello.py
我复制了下面的行Expected
and Got
进入 WinMerge 以排除我无法发现的字符中的一些细微差异;它告诉我它们是相同的。
但是,如果我重做命令行运行,但将输出重定向到文本文件,如下所示:
> x:\my_virtualenv\Scripts\python.exe -m doctest -v hello.py > out.txt
测试仍然失败,但最终的失败输出有点不同:
Failed example:
test_func()
Expected:
u'☃'
Got:
u'\u2603'
如果我将转义的 unicode 文字放入我的文档测试中:
# coding=utf-8
def test_func():
u"""
>>> test_func()
u'☃'
"""
return u'\\u2603'
测试通过。但据我所知,u'\u2603'
and u'☃'
应该评估相同的事情。
对于这个失败的案例,我确实有两个问题:
- 是医生给出的陈述之一(根据
Expected
or Got
)对于这种情况下 docester 的值不正确? (IE。x != eval(repr(x))
)
- 如果不是,为什么测试失败?