问题描述
我有以下doctest编写为 x.doctest
:
I have the following doctest written x.doctest
:
This is something:
>>> x = 3 + 4
foo bar something else:
>>> from __future__ import division
>>> y = 15
>>> z = int('24')
>>> m = z / y
>>> print (m)
1.6
但是当我运行 python时- m在python 2.7.11上的doctest x.doctest
中,doctest无法识别__future__进口部门的
:
But when I ran python -m doctest x.doctest
on python 2.7.11, the doctest didn't recognize from __future__ import division
:
**********************************************************************
File "x.doctest", line 11, in x.doctest
Failed example:
print (m)
Expected:
1.6
Got:
1
**********************************************************************
1 items had failures:
1 of 6 in x.doctest
***Test Failed*** 1 failures.
即使我将未来的导入语句移到第一行:
Even when I shifted the future import statement to the first line:
This is something:
>>> from __future__ import division
>>> x = 3 + 4
foo bar something else:
>>> y = 15
>>> z = int('24')
>>> m = z / y
>>> print (m)
1.6
doctest仍然失败:
The doctest still fails:
**********************************************************************
File "x.doctest", line 11, in x.doctest
Failed example:
print (m)
Expected:
1.6
Got:
1
**********************************************************************
1 items had failures:
1 of 6 in x.doctest
***Test Failed*** 1 failures.
为什么会这样,我该如何解决?
是否有doctest标志/选项,要求确保__future__进口部门的
被识别?
Is there a flag / option for doctest that asks ensures that from __future__ import division
is recognized?
注意:我可以强制检查 print(int(m))
或 y = 15。
会通过doctest,但这不是很理想。
Note: I could just force the check on print (int(m))
or y = 15.
and the doctest will pass but that is not that desirable.
推荐答案
Doctests通过Python编译器单独运行每行 。这意味着在doctest本身中用从__future__ import ..
语句声明的所有编译器标志都是无效的。
Doctests run each line in isolation through the Python compiler. This means that any compiler flags specified with a from __future__ import ..
statement in the doctest itself is useless in a doctest.
但是,您可以从真实的到您的doctest全局变量。如果您不使用__future__ import< name> 格式的,但使用
import __future __
,而是导入该实际模块,并可以将其定义的对象的引用添加到doctest globs
或 extraglobs
字典:
However, you can add names from the real __future__
module to your doctest globals. If you don't use the from __future__ import <name>
format but use import __future__
instead, you import that actual module, and can add references to the objects it defines to the doctest globs
or extraglobs
dictionaries:
if __name__ == "__main__":
import doctest
import __future__
doctest.testmod(extraglobs={'division': __future__.division})
The <$ c然后,$ c> DocTestRunner 将为您设置正确的编译器标记,这些标记来自于其中。
The DocTestRunner
will then set the right compiler flags for you when compiling individual lines from these.
演示:
>>> import doctest
>>> import __future__
>>> import sys
>>> def foo():
... """
... >>> 1 / 2
... 0.5
... """
...
>>> doctest.testmod(sys.modules['__main__'])
**********************************************************************
File "__main__", line 3, in __main__.foo
Failed example:
1 / 2
Expected:
0.5
Got:
0
**********************************************************************
1 items had failures:
1 of 1 in __main__.foo
***Test Failed*** 1 failures.
TestResults(failed=1, attempted=1)
>>> doctest.testmod(sys.modules['__main__'], extraglobs={'division': __future__.division})
TestResults(failed=0, attempted=1)
这篇关于Doctest无法识别__future __。division的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!