python 带有 pytest 标记的脚本的 Sphinx 文档

Sphinx documentation of python script with pytest marker

我一直在考虑使用 Sphinx 来记录使用 pytest 的测试模块。在脚本中有用户定义的pytest.mark。有没有办法配置 Sphinx 或 .rst 模板文件以包含 pytest.mark 定义,即使它们在文档字符串之外?

关于 :regexp: 使用的 sphinx 文档非常模糊,我一直无法找到使用它的好例子。

Python 2.7.6 Sphinx-build 1.3.3

"""
    script: test_sample.py
    :Test Description: Simple test to show function of py.test and sphinx doc
    :Author: James Brown
    Requirement ID: Test1234
"""
import sys
from sys import argv
import re
import pytest


pytestmark = [
    pytest.mark.author("jbrown"),
    pytest.mark.req("test-001"),
    pytest.mark.testtype("Full"),
    pytest.mark.mat0]

blah = """
    blah
    blah
    blah
"""


def func(x):
    """
        Returns an integer incremented by one
    """

    return x + 1

def test_answer():
    """
        Checks if value equals 5
    """
    assert func(3) == 5

第一个文件添加

.. exec::
    import re
    from src.test_samplepytest import pytestmark

    attre = re.compile(r"\'(\w+)\'.*\(\'?(\w*)")

    for value in pytestmark:
        mark = str(value)
        att = attre.search(mark)
        if att.group(2):
            print "{:<10}: {:<15}\n".format(att.group(1),att.group(2))
        else:
            print "{:<10}: \n".format(att.group(1))'

我选择为测试脚本编写一个文件解析器。这会运行验证、pylint 等,并将有趣的信息加载到数据库中 table。

然后我可以查询数据库,使用 https://pypi.python.org/pypi/tabulate.

返回格式化的 table

sphinx 第一个文件包含:

  5     .. exec::                                                                  
  6         dbquery('col1,col2,col3','test_moduleName',db='my_database')