为什么 doctest 在使用 tesfile 执行时找不到定义在同一文件上的函数?
Why doctest can't find functions defined on the same file when executed with tesfile?
我有一个包裹 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
# example.txt
>>> from foo import bar
>>> 1
1
>>> bar()
'bar'
# foo.py
def bar():
return "bar"
现在 python 解释器将显示 0 个失败的测试行。
import doctest
doctest.testfile('example.txt')
>>> TestResults(failed=0, attempted=3)
但是,如果我省略行 >>> from foo import bar
,我将得到以下错误
>>> doctest.testfile('example.txt')
**********************************************************************
File "./example.txt", line 6, in example.txt
Failed example:
bar()
Exception raised:
Traceback (most recent call last):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/doctest.py", line 1329, in __run
exec(compile(example.source, filename, "single",
File "<doctest example.txt[1]>", line 1, in <module>
bar()
NameError: name 'bar' is not defined
**********************************************************************
1 items had failures:
1 of 2 in example.txt
***Test Failed*** 1 failures.
TestResults(failed=1, attempted=2)
我有一个包裹 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
# example.txt
>>> from foo import bar
>>> 1
1
>>> bar()
'bar'
# foo.py
def bar():
return "bar"
现在 python 解释器将显示 0 个失败的测试行。
import doctest
doctest.testfile('example.txt')
>>> TestResults(failed=0, attempted=3)
但是,如果我省略行 >>> from foo import bar
,我将得到以下错误
>>> doctest.testfile('example.txt')
**********************************************************************
File "./example.txt", line 6, in example.txt
Failed example:
bar()
Exception raised:
Traceback (most recent call last):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/doctest.py", line 1329, in __run
exec(compile(example.source, filename, "single",
File "<doctest example.txt[1]>", line 1, in <module>
bar()
NameError: name 'bar' is not defined
**********************************************************************
1 items had failures:
1 of 2 in example.txt
***Test Failed*** 1 failures.
TestResults(failed=1, attempted=2)