gpt4 book ai didi

python - 为什么 doctests 在使用 Sphinx 的 `make doctest` 运行时会引发 NameError ?

转载 作者:行者123 更新时间:2023-11-28 16:37:06 25 4
gpt4 key购买 nike

我有一个简单的 function with a doctest ,当使用 Sphinx 的 make doctest 运行时,出现以下错误:

File "scheemey.rst", line ?, in default
Failed example:
verify_balanced('asdf (foo [bar] [[baz], {}, ()]')
Exception raised:
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 1315, in __run
compileflags, 1) in test.globs
File "<doctest default[0]>", line 1, in <module>
verify_balanced('asdf (foo [bar] [[baz], {}, ()]')
NameError: name 'verify_balanced' is not defined

这可能是什么原因造成的?

最佳答案

如果没有正确导入具有测试功能的模块,我可以重现问题中的错误。

要使其正常工作,您可以使用 testsetup指令:

.. testsetup:: 

from yourmodule import verify_balanced

>>> verify_balanced('asdf (foo) [bar] [[baz], {}, ()]')
>>> verify_balanced('asdf (foo [bar] [[baz], {}, ()]')
5

请注意,doctest 会忽略 None 返回值(请参阅 Python doctests: test for None)。

关于python - 为什么 doctests 在使用 Sphinx 的 `make doctest` 运行时会引发 NameError ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24590029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com