fastmock

FastAPI middleware for mocking responses based on Pydantic models response


Keywords
fastapi, mock, python
License
MIT
Install
pip install fastmock==0.1.1

Documentation

fastmock

FastMock framework, mock your FastAPI APIs

Test Coverage Package version


Documentation: https://tpemeja.github.io/fastmock

Source Code: https://github.com/tpemeja/fastmock


FastMock is a Python framework designed to mock FastAPI APIs based on response models.

Key features of this project include:

  • Ease of Use: Simply add the middleware to start mocking your APIs.
  • Flexibility: Mocking can be customized through various parameters, such as activation, data generation, length, status codes, and more.
  • Modularity: The package includes default functions to retrieve mocking parameters from API declarations to request headers. However, all these functions can be modified, allowing you to create your own custom functions.

Requirements

This package is build to be used with FastAPI, please learn on to use it on their well documented website - FastAPI Documentation

To mock an API, you need to describe the API using FastAPI's decorator key responses.

For example:

@app.get("/",
         status_code=status.HTTP_200_OK,
         responses={
             status.HTTP_200_OK: {"model": list[int]},
             status.HTTP_404_NOT_FOUND: {"model": str}
         })

Installation

$ pip install fastmock

---> 100%

Example

Create it

  • Create a file main.py with:
from fastapi import FastAPI, status
from pydantic import BaseModel

from fastmock.decorator import FastMockDecorator
from fastmock.middleware import FastMockMiddleware

app = FastAPI()
app.add_middleware(FastMockMiddleware)


mock = FastMockDecorator()

class Item(BaseModel):
    name: str
    price: float
    is_offer: bool | None = None


@app.get("/",
         status_code=status.HTTP_200_OK,
         responses={
             status.HTTP_200_OK: {"model": dict[str, str]}
         })
def read_root():
    return {"Hello": "World"}


@mock(element_size=3)
@app.get("/items",
         status_code=status.HTTP_200_OK,
         responses={
             status.HTTP_200_OK: {"model": list[Item]}
         })
def read_items():
    return []
  • Start the server with uvicorn main:app

Check it

Open your browser at http://127.0.0.1:8000/items.

You will see the JSON response as:

[
  {
    "name": "fuquvERvYTfWVEbYRKgi",
    "price": 18164954265977.8,
    "is_offer": null
  },
  {
    "name": "akMCejCxOhMjgGMPMrcb",
    "price": 40.3130726635657,
    "is_offer": null
  },
  {
    "name": "uEONHBXGCirPDrLJKgXu",
    "price": -9.23356705084994,
    "is_offer": null
  }
]

You just created an API using FastAPI that:

  • Return value generated from defined response model
  • Modify output list size using decorator

Inspiration

  • A project idea that came from my use of FastAPI by tiangolo, the GitHub project inspired me for the structure and documentation of this project.
  • Ideas and code for data generation with Faker inspired by NiyazNz in the fastapi-mock-middleware project.

License

This project is licensed under the terms of the MIT license.