如何测试您的 Sphinx 文档的有效性?

Dan*_*inn 7 python testing python-sphinx

我有一大堆使用标准 Sphinx.rst文件编写的 Python 包文档。我还对我的包进行了测试,其中我想包括一个测试,以确定文档是否可以正确编译为预期的输出。基本上,当我使用无处链接或标题格式不佳等时,我想捕获案例。

现在我知道我总是可以编写一个调用make html和测试退出代码的测试,但这感觉真的很脏,所以我认为必须有更好的方法。有人知道这是什么吗?

Mar*_*mro 4

您可以采用与为代码创建单元测试相同的方式为文档创建单元测试。warningiserror=True要捕获警告,您可以在 Sphinx 配置中设置:

from django.utils import unittest
from sphinx.application import Sphinx


class DocTest(unittest.TestCase):
    source_dir = u'docs/source'
    config_dir = u'docs/source'
    output_dir = u'docs/build'
    doctree_dir = u'docs/build/doctrees'
    all_files = 1

    def test_html_documentation(self):
        app = Sphinx(self.source_dir,
                     self.config_dir,
                     self.output_dir,
                     self.doctree_dir,
                     buildername='html',
                     warningiserror=True,
        )
        app.build(force_all=self.all_files)
        # TODO: additional checks here if needed

    def test_text_documentation(self):
        # The same, but with different buildername
        app = Sphinx(self.source_dir,
                     self.config_dir,
                     self.output_dir,
                     self.doctree_dir,
                     buildername='text',
                     warningiserror=True,
        )
        app.build(force_all=self.all_files)
        # TODO:  additional checks if needed

    def tearDown(self):
        # TODO: clean up the output directory
        pass
Run Code Online (Sandbox Code Playgroud)