FastMock framework, mock your FastAPI APIs
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.
- Fault Injection: Simulate latency and failures per-route or via request header, so you can test how your client handles a slow or unreliable API without writing any extra code.
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.pywith:
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
How does this compare to other FastAPI mocking libraries?
A couple of other projects cover similar ground. Here's how fastmock differs:
| fastmock | fastapi-mock | fastapi-mock-middleware | |
|---|---|---|---|
| Trigger | Decorator / middleware config | raise NotImplementedError(), or catches a 500 |
Middleware, per unimplemented route |
| Per-route status code override | ✅ | – | – |
| Runtime overrides via request headers | ✅ | – | – |
| Configurable data retrieval precedence chain | ✅ | – | – |
| Multiple examples, randomly chosen | – | ✅ | – |
| Fault injection (delay / failure rate) | ✅ | – | – |
| Pydantic 2 | ✅ | ✅ | – |
| Actively maintained | ✅ | ✅ | ❌ (last commit 2024) |
fastapi-mock is a great fit if you want the absolute lowest-friction way to stub an endpoint you haven't implemented yet — no decorator, no config, just raise NotImplementedError(). fastmock trades a little more setup for per-route and per-request control over data shape, status codes, and failure modes, which matters once you're mocking for QA, staging, or demo environments rather than just a placeholder.
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.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fastmock-0.1.3.tar.gz | 684.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastmock-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 693.9 kB
Release files / fastmock-0.1.3.tar.gz
| Download URL | fastmock-0.1.3.tar.gz |
|---|---|
| Size | 684.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
17e4a3abcc6f2060d26696c6354a5ec0cdb440a526f69e5cdad087612ccfe384
|
|
BLAKE2b-256 checksum How to use checksums |
a5e83ff7cd52ca7df1e7d7dfeb4f180e739ecd779feffbcd1aceb81854a302c4
|
| 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.3-py3-none-any.whl
| Download URL | fastmock-0.1.3-py3-none-any.whl |
|---|---|
| Size | 10.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
13b8c47679b6948d387569221dc1af3f79aba220032503553d268da2bbea5c74
|
|
BLAKE2b-256 checksum How to use checksums |
86cddfe1f064888189fc6368f6f24a8ca01d5e2ea7d27bc4d8ea6289ff1834fb
|
| 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}
|