【发布时间】:2017-09-28 09:17:36
【问题描述】:
我希望我的代码在 Python 2 和 3 中工作。我使用 doctest 和
from __future__ import unicode_literals
是否有一个我可以设置的标志/一个插件使它忽略 Python 2 具有用于 unicode 字符串的 u 前缀?
示例
一个测试在 Python 3 中有效,但在 Python 2 中失败:
Expected:
'Me \\& you.'
Got:
u'Me \\& you.'
小例子
from __future__ import unicode_literals
def foo():
"""
Returns
-------
unicode - for Python 2 and Python 3
Examples
--------
>>> foo()
'bar'
"""
return 'bar'
if __name__ == '__main__':
import doctest
doctest.testmod()
【问题讨论】:
-
在这种情况下,问题可能不是“
u前缀”,而是"..."与 @987654327 的 type 不同@。比较失败,因为您正在针对unicode值测试str值。 -
因为
u"Me \\& you."而不是u'Me \\& you.'也失败了,我很确定 doctests 会直接比较字符串。 -
请提供一个最小的例子来重现这个问题。
-
好吧,
unicode_literals意味着隐含的所有文字都是u''文字。我想这不适用于 doctest,因为在 docstring 中写入的值不是文字(它是文字中的文字)并且 doctest 不考虑导入的unicode_literals。如果您将预期值声明为u'bar',它会起作用。 -
@deceze 不,将预期值声明为
u'bar'是行不通的。问题是我希望它像我在问题的第一句话中所写的那样在 Python 2 和 3 上运行。
标签: python-2.7 unicode pytest doctest