Skip to content

FastAPI mock middleware

license pypi test codecov

FastAPI middleware for mocking response data of non-implemented endpoints.

Mock data is generated in accordance with endpoint return type or provided response_model using polifactory.


For more information on how to use fastapi-mock-middleware, please refer to the official documentation.

Installation

pip install fastapi-mock-middleware

Usage example

Add MockAPIMiddleware middleware to app and raise APINotImplementedError in your endpoint stubs.

import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel

from fastapi_mock_middleware import MockAPIMiddleware, APINotImplementedError

app = FastAPI()
app.add_middleware(MockAPIMiddleware)


class Item(BaseModel):
    id: int
    name: str


@app.get('/')
async def list_items() -> list[Item]:
    raise APINotImplementedError()


if __name__ == '__main__':
    uvicorn.run('example:app', reload=True)

Check the response using curl.

curl http://127.0.0.1:8000/

Called API must return mocked data:

[
  {
    "id": 5392,
    "name": "gVzyVVUmGGevXlQvXGBW"
  }
]