Python doctests / sphinx:风格指南,如何使用它们并拥有可读的代码?

发布于 2024-09-03 00:41:28 字数 690 浏览 10 评论 0原文

我喜欢 doctests,它是我使用的唯一测试框架,因为它编写起来非常快,而且因为与 sphinx 一起使用,它几乎不费吹灰之力就能制作出如此出色的文档......

但是,很多时候,我最终会做类似的事情this :

"""
Descriptions
=============

bla bla bla ...

    >>> test
    1
bla bla bla + tests tests tests * 200 lines = poor readability of the actual code
"""

我的意思是,我将所有带有文档说明的测试放在模块的顶部,因此您必须愚蠢地滚动才能找到实际的代码,这非常难看(在我看来)。但是,我认为文档测试仍应保留在模块中,因为您应该能够在阅读源代码时阅读它们。 那么我的问题来了:sphinx/doctests爱好者,你们如何组织你们的doctests,比如代码可读性不受影响?是否有针对 doctests、sphinx 的风格指南?对于 sphinx 的文档字符串,您使用 google 或 sphinx style-guide 还是其他什么?

I love doctests, it is the only testing framwork I use, because it is so quick to write, and because used with sphinx it makes such great documentations with almost no effort...

However, very often, I end-up doing things like this :

"""
Descriptions
=============

bla bla bla ...

    >>> test
    1
bla bla bla + tests tests tests * 200 lines = poor readability of the actual code
"""

What I mean is that I put all my tests with documentation explanations on the top of the module, so you have to scroll stupidly to find the actual code, and this is quite ugly (in my opinion). However, I think that the doctests should still stay in the module, because you should be able to read them while reading the source code.
So here comes my question : sphinx/doctests lovers, how do you organize your doctests, such as the code readability doesn't suffer ? Is there a style guide for doctests, for sphinx ? For docstring with sphinx, do you use google or sphinx style-guide or something else ?

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

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

发布评论

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

评论(1

喵星人汪星人 2024-09-10 00:41:28

我认为文档测试有两种。

  1. 您可以在该函数的文档字符串中添加一些内容,但如果是这样,请保持简短。
  2. 另一个选项是完整的文档/教程,我将其作为单独的文件进行。

与普通文档不同,文档测试的美妙之处在于,即使它们不在同一屏幕中,您也可以确保它们与代码保持同步。阅读代码时,您希望看到代码,也许带有一些描述性文本。在阅读教程时,您不想只看到代码而是示例。

I think there are two sorts of doctest.

  1. You can put something in the docstring for the function, but if so keep it short and simple.
  2. The other option is full documentation/tutorial, and I do that as a separate file.

Unlike ordinary documentation, the beauty of doctesting is that you can be sure they are going to stay in sync with the code even if they aren't in the same screen. When reading the code you want to see code, perhaps with a little descriptive text. When reading a tutorial you don't want to see the code just the examples.

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