How to exclude pytest test_* functions from Sphinx autodoc?

时间:2016-08-31 12:19:09

标签: python python-sphinx

I have multiple modules in my package.

package/
|--mod1.py
|--mod2.py

Each module contains some functions and a test_function for testing the module.

I am using sphinx-apidoc to generate the .rst files for every module in package. My problem is that when I am generating documentation for my package, the test functions are also getting included in the documentation. I know it is possible to skip functions by using: :exclude members: function. But I'm looking for a solution that will allow me to do that for all modules by using a pattern similar to test_*.

My package.rst file looks like this:

package package
===============

Submodules
----------

.. toctree::

   package.mod1
   package.mod2

Module contents
---------------

.. automodule:: package
    :members:
    :undoc-members:
    :show-inheritance:

And my mod1.rst file looks like this:

package.mod1 module
===================

.. automodule:: package.mod1
    :members:
    :undoc-members:
    :show-inheritance:

Thanks in advance.

1 个答案:

答案 0 :(得分:1)

虽然@mzjn绝对是一个很好的指针,但我仍然为此苦了一段时间。

解决方案是在autodoc-skip-member文件中为autodoc-skip-member事件编写一个处理程序并将其连接到conf.py事件。

这就是后者的样子:

# conf.py

# Loads of configuration settings

# This is the expected signature of the handler for this event, cf doc
def autodoc_skip_member_handler(app, what, name, obj, skip, options):
    # Basic approach; you might want a regex instead
    return name.startswith("test_")

# Automatically called by sphinx at startup
def setup(app):
    # Connect the autodoc-skip-member event from apidoc to the callback
    app.connect('autodoc-skip-member', autodoc_skip_member_handler)