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

我有一个仅包含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从文档字符串自动生成文档?

jbs1zgh 回答:是否可以自动生成pytest测试的文档?

您可以使用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

,

使用狮身人面像时,应将测试文件夹添加到conf.py文件中的Python路径中:

sys.path.insert(0,os.path.abspath(os.path.join(os.path.dirname(__file__),'..','tests')))

然后在每个第一个文件中,您都可以简单地编写:

.. automodule:: test_basic_auth_app
   :members:

如果您还要记录测试结果,请查看Sphinx-Test-Reports

本文链接:https://www.f2er.com/3094787.html

大家都在问