如何让Sphinx测试文档中嵌入的代码?
如果这段代码是在我的文档中的引用部分,我需要做些什么才能让Sphinx在生成文档时运行它呢?我试着在某个.rst
文件里添加了
.. testcode::
import datetime
def today():
return datetime.datetime.now().date()
if True:
today()
并且在conf.py
中加入了Sphinx的doctest扩展,但我遇到了一个错误,提示函数体的第一行缩进不正确。Sphinx的doctest能运行文档中定义的函数吗?
1 个回答
4
这个 testcode
指令需要有一个对应的 testoutput
指令。
下面是文档中的一个例子。
.. testcode::
print 'Output text.'
.. testoutput::
:hide:
:options: -ELLIPSIS, +NORMALIZE_WHITESPACE
Output text.