【问题标题】:How can I run Sphinx doctests as part of setup.py?如何将 Sphinx 文档测试作为 setup.py 的一部分运行?
【发布时间】:2015-07-24 14:22:04
【问题描述】:

我想在我的 setup.py 包中包含一个 setuptools 命令来运行 Sphinx doctest,如下所示:

$ python setup.py sphinx_doctest

我的包结构如下:

my_pkg
|---__init__.py
|---module1.py    # Containing reStructuredText docstrings with examples
docs
|---build
|---|---doctrees
|---source
|---|---conf.py   # Sphinx config
|---|---index.rst # Sphinx index file
setup.py

我怎样才能实现一个 setuptools 命令,它相当于:

$ sphinx-build -b doctest -d docs/build/doctrees docs/source docs/build

【问题讨论】:

    标签: python python-sphinx setuptools setup.py doctest


    【解决方案1】:

    子类setuptools.Command 并使用sphinx.application.Sphinx 启动sphinx.ext.doctest 构建器。

    在 setup.py 中:

    from setuptools import setup, Command
    
    class Doctest(Command):
        description = 'Run doctests with Sphinx'
        user_options = []
    
        def initialize_options(self):
            pass
    
        def finalize_options(self):
            pass
    
        def run(self):
            from sphinx.application import Sphinx
            sph = Sphinx('./docs/source', # source directory
                         './docs/source, # directory containing conf.py
                         './docs/build', # output directory
                         './docs/build/doctrees', # doctree directory
                         'doctest') # finally, specify the doctest builder
            sph.build()
    
    sphinx_requires = ['sphinx>=1.3.1']
    
    setup(
        name='mypkg',
        version='0.0.1',
        description='My Package',
        packages=['mypkg'],
        cmdclass={
            'doctests': Doctest
        },
        extras_require={
            'build_sphinx': sphinx_requires,
        },
    )
    

    【讨论】:

      猜你喜欢
      • 2013-03-04
      • 1970-01-01
      • 2020-07-15
      • 2019-11-28
      • 2017-12-30
      • 2011-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多