Skip to main content

An ASGI middleware to populate OpenAPI Specification examples from pytest functions

Project description

PyTest-API: Populate OpenAPI Examples from Python Tests

purpose PyPI

PyTest-API is an ASGI middleware that populates OpenAPI-Specification examples from pytest functions.

Installation

pip install pytest-api

or

poetry add --dev pytest-api

How to use it:

Starting with test_main.py file:

from .main import spec


@spec.describe
def test_default_route(client):
    """
    GIVEN
    WHEN root endpoint is called with GET method
    THEN response with status 200 and body OK is returned
    """
    response = client.get("/")
    assert response.status_code == 200
    assert response.json() == {"message": "OK"}

Impliment solution in /main.py file:

from fastapi import FastAPI

from pytest_api import SpecificationMiddleware

app = FastAPI()
spec = SpecificationMiddleware

app.add_middleware(spec)

app.openapi = spec.custom_openapi


@app.get("/")
def default_route():
    return {"message": "OK"}

Run FastAPI app:

poetry run uvicorn test_app.main:app --reload

Open your browser to http://localhost:8000/docs#/ too find the doc string is populated into the description.

Your doc string will now be populated into the description.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_api-0.1.2.tar.gz (3.3 kB view hashes)

Uploaded Source

Built Distribution

pytest_api-0.1.2-py3-none-any.whl (3.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page