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
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 the FastAPI app via:
uvicorn main:app --reload
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.1.tar.gz
(3.0 kB
view hashes)
Built Distribution
Close
Hashes for pytest_api-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88fd3bd0707b8f0465372ae4bee230cddaf9036ccac875842a8d5a80c676ebd2 |
|
MD5 | 2db2eed21e4ef8228ee972630a817e4f |
|
BLAKE2b-256 | 962a23b7bc613fa3c69cbe2d6238008a68f7ed479ede19ada12430b671bf312a |