如何让 Sphinx 测试文档中嵌入的代码?

发布于 2024-08-17 10:49:21 字数 401 浏览 2 评论 0原文

如果此代码是我的文档中的块引用,那么我需要做什么才能使 Sphinx 在文档生成了吗?我尝试将

.. testcode::

    import datetime
    def today():
        return datetime.datetime.now().date()
    if True:
        today()

.rst 源代码和 Sphinx doctest 扩展添加到 conf.py 中,但收到错误消息,抱怨函数第一行出现意外缩进身体。 Sphinx的doctest能够运行文档中定义的函数吗?

If this code is a blockquote in my documentation, what do I need to do to make Sphinx run it when the documentation is generated? I tried adding

.. testcode::

    import datetime
    def today():
        return datetime.datetime.now().date()
    if True:
        today()

to one of the .rst sources and the Sphinx doctest extension to conf.py, but I got an error complaining about unexpected indentation on the first line of the function body. Is Sphinx's doctest able to run functions defined in the documentation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聽兲甴掵 2024-08-24 10:49:21

testcode 指令需要匹配的 testoutput 指令。

这是文档中的示例。

.. testcode::

   print 'Output     text.'

.. testoutput::
   :hide:
   :options: -ELLIPSIS, +NORMALIZE_WHITESPACE

   Output text.

The testcode directive needs a matching testoutput directive.

Here's the example from the documentation.

.. testcode::

   print 'Output     text.'

.. testoutput::
   :hide:
   :options: -ELLIPSIS, +NORMALIZE_WHITESPACE

   Output text.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文