我一直在考虑使用 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 等,并将感兴趣的信息加载到数据库表中。
然后我可以查询数据库,使用 https://pypi.python.org/pypi/tabulate 返回格式化表.
sphinx 第一个文件包含:
5 .. exec::
6 dbquery('col1,col2,col3','test_moduleName',db='my_database')
关于python - 带有 pytest 标记的 python 脚本的 Sphinx 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34690171/