Skip to main content

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%

Or, if you use uv:

$ uv add 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.

Contributing

This project uses uv for dependency management. To set up a development environment, run the tests, or cut a release, see the Contributing guide.

License

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

Metadata

Release files for fastmock 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastmock 0.1.2
File Size Uploaded
fastmock-0.1.2.tar.gz 681.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastmock 0.1.2
File Interpreter ABI Platform
fastmock-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 690.8 kB

Release files / fastmock-0.1.2.tar.gz

Download URL fastmock-0.1.2.tar.gz
Size 681.9 kB
Tags Source
SHA-256 checksum
How to use checksums
49593361826980fb8aa644b658f851588ae0438a9029241a48467c35d6d8cd4b
BLAKE2b-256 checksum
How to use checksums
2b03b96a926f0eef1f7b22e8fcee961f4935a44c4f9d5284b2e3e60779b79f5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / fastmock-0.1.2-py3-none-any.whl

Download URL fastmock-0.1.2-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d272c13f0d093a5dd5975d662dabd7b55ecb69422bea446aa394bcee3c4219db
BLAKE2b-256 checksum
How to use checksums
a57b20bd2b9bc84383a4ef9cedee13d64a419adf772d7bdc571cb3657d7f5da7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

0.2.0

2 release files

0.1.3

2 release files

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page