Auto generate doctest output with Sphinx extension

半世苍凉 提交于 2019-11-28 01:05:35

I have to strongly (but kindly) advise against what you're trying to do.

What you're asking is against the "test part" of the doctest module:

The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown.

These tests have a reasons to be if you write the input and the expected output and let Python check if the expected output match the actual output.

If you let Python produce the expected output, well.. it will no longer be expected (by the user/author), so the doctests will never fail, hence those tests will be useless.

Note: If inside a function there's no logic (if/else, while-loops, appends, etc..) there's no need to test them. And tests must not reproduce the testing logic, otherwise they're not testing the function anymore.

I found this video about test driven development very interesting, maybe it could be of interest to you if you want to know more about this argument.

mzjn

Here is a suggestion on how you could achieve what I suspect you might be looking for:

Doug Hellmann has written an interesting article called Writing Technical Documentation with Sphinx, Paver, and Cog. It has a section describing how the Cog tool can be used to automatically run code examples and capture the output for inclusion in Sphinx-built documentation.


There is also a contributed Sphinx extension called autorun that can execute code in a special runblock directive and attach the output to the documentation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!