我有一个包mypackage
,其中我有foo.py
# mypackage/foo.py
def foo():
"""
>>> 1
1
>>> bar()
'bar'
"""
pass
def bar():
return "bar"
如果我运行python -m doctest mypackage/foo.py
,它就像一个符咒,但是。如果我创建一个带有
import doctest
doctest.testfile("foo.py", package="mypackage")
。。。在与mypackage相同的文件夹中,并运行此文件,它将失败
**********************************************************************
File "/Users/gecko/code/python/doctesttest/mypackage/foo.py", line 6, in foo.py
Failed example:
bar()
Exception raised:
Traceback (most recent call last):
File "/Users/gecko/.pyenv/versions/3.8.2/lib/python3.8/doctest.py", line 1329, in __run
exec(compile(example.source, filename, "single",
File "<doctest foo.py[1]>", line 1, in <module>
bar()
NameError: name 'bar' is not defined
**********************************************************************
1 items had failures:
1 of 2 in foo.py
***Test Failed*** 1 failures.
➜ doctesttest
我的问题是为什么,以及如何解决
只需确保在运行doctest时导入
bar
。例如,在同一个目录中,我有example.txt
和foo.py
现在python解释器将显示0条失败的测试行
但是,如果省略
>>> from foo import bar
行,将出现以下错误相关问题 更多 >
编程相关推荐