An ASGI middleware to populate OpenAPI Specification examples from pytest functions
Project description
PyTest-API: Populate OpenAPI Examples from Python Tests
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.
Project details
Release history Release notifications | RSS feed
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)
Built Distribution
Close
Hashes for pytest_api-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a59037fff89e2a8205dafa0e8eec02ae8bdf3f7c9f5a60a814f23d12498a3a97 |
|
MD5 | f65fec22d52c48c41e1fae0452695158 |
|
BLAKE2b-256 | a2b97a53d436b83d1e3c9031f37ba5e94e92a0b95412d6e6c53e09b20583d3db |