是否可以为pytest测试自动生成文档?

问题描述 投票:0回答:1

我有一个仅包含pytest测试的项目,而没有模块或类来测试远程项目。例如。结构->

.
├── __init__.py
├── test_basic_auth_app.py
├── test_basic_auth_user.py
├── test_advanced_app_id.py
├── test_advanced_user.py
└── test_oauth_auth.py

测试看起来像

"""
Service requires credentials (app_id, app_key) to be passed using the Basic Auth

"""
import base64

import pytest

import authorising.auth
from authorising.resources import Service


@pytest.fixture(scope="module")
def service_settings(service_settings):
    "Set auth mode to app_id/app_key"
    service_settings.update({"backend_version": Service.Auth_app})
    return service_settings

def test_basic_auth_app_id_key(application):
    """Test client access with Basic HTTP Auth using app id and app key

    Configure Api/Service to use App ID / App Key Authentication
    and Basic HTTP Auth to pass the credentials.
    """

    credentials = application.authobj.credentials
    encoded = base64.b64encode(
        f"{creds['app_id']}:{credentials['app_key']}".encode("utf-8")).decode("utf-8")

    response = application.test_request()

    assert response.status_code == 200
    assert response.request.headers["Auth"] == "Basic %s" % encoded

是否有可能例如使用Sphinx从文档字符串自动生成文档?

pytest documentation python-sphinx
1个回答
0
投票

您可以使用sphinx-apidoc使用python-docstrings自动生成测试文档

例如,如果您具有如下目录结构

.
docs
  |-- rst
  |-- html
tests
  ├── __init__.py
  ├── test_basic_auth_app.py
  ├── test_basic_auth_user.py
  ├── test_advanced_app_id.py
  ├── test_advanced_user.py
  └── test_oauth_auth.py
sphinx-apidoc -o docs/rst tests
sphinx-build  -a -b  html docs/rst docs/html -j auto 

您所有的文档HTML文件将在docs/html下。

sphinx-apidoc支持多个选项。这是[链接]:https://www.sphinx-doc.org/en/master/man/sphinx-apidoc.html

© www.soinside.com 2019 - 2024. All rights reserved.