requests-mock-flask
requests-mock-flask helps with testing Flask applications with httpretty, respx (for httpx and HTTPX2), responses or requests-mock.
Installation
Requires Python 3.12+.
pip install requests-mock-flask
Usage examples
"""Examples of using requests-mock-flask."""
from http import HTTPStatus
import flask
import httpretty
import httpx
import httpx2
import requests
import requests_mock
import responses
import respx
from requests_mock_flask import add_flask_app_to_mock
app = flask.Flask(import_name="example_app")
@app.route(rule="/")
def _() -> str:
"""Return a simple message."""
return "Hello, World!"
# Using responses
with responses.RequestsMock(assert_all_requests_are_fired=False) as resp_m:
add_flask_app_to_mock(
mock_obj=resp_m,
flask_app=app,
base_url="http://www.example.com",
)
response = requests.get(url="http://www.example.com", timeout=30)
assert response.status_code == HTTPStatus.OK
assert response.text == "Hello, World!"
# Using requests-mock
with requests_mock.Mocker() as req_m:
add_flask_app_to_mock(
mock_obj=req_m,
flask_app=app,
base_url="http://www.example.com",
)
response = requests.get(url="http://www.example.com", timeout=30)
assert response.status_code == HTTPStatus.OK
assert response.text == "Hello, World!"
# Using a requests-mock adapter
session = requests.Session()
adapter = requests_mock.Adapter()
session.mount(prefix="mock", adapter=adapter)
add_flask_app_to_mock(
mock_obj=adapter,
flask_app=app,
base_url="mock://www.example.com",
)
response = session.get(url="mock://www.example.com", timeout=30)
assert response.status_code == HTTPStatus.OK
assert response.text == "Hello, World!"
# Using httpretty
with httpretty.core.httprettized():
add_flask_app_to_mock(
mock_obj=httpretty,
flask_app=app,
base_url="http://www.example.com",
)
response = requests.get(url="http://www.example.com", timeout=30)
assert response.status_code == HTTPStatus.OK
assert response.text == "Hello, World!"
# Using respx
with respx.mock(assert_all_called=False) as respx_mock:
add_flask_app_to_mock(
mock_obj=respx_mock,
flask_app=app,
base_url="http://www.example.com",
)
httpx_response = httpx.get(url="http://www.example.com")
assert httpx_response.status_code == HTTPStatus.OK
assert httpx_response.text == "Hello, World!"
# Using respx with httpx2
#
# ``pytest-httpx2`` registers the ``httpcore2`` mocker with ``respx``.
# It is loaded automatically under pytest; import it elsewhere.
with respx.mock(assert_all_called=False, using="httpcore2") as respx_mock:
add_flask_app_to_mock(
mock_obj=respx_mock,
flask_app=app,
base_url="http://www.example.com",
)
httpx2_response = httpx2.get(url="http://www.example.com")
assert httpx2_response.status_code == HTTPStatus.OK
assert httpx2_response.text == "Hello, World!"
Use cases
Use requests or other Python APIs for testing Flask applications.
Create a test suite which can test a Flask application as well as a live web application, to make a verified fake.
Test a service which calls a Flask application that you have the source code for.
Full documentation
See the full documentation for more information including how to contribute.
Metadata
Release files for requests-mock-flask 2026.10.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| requests_mock_flask-2026.10.9.tar.gz | 42.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| requests_mock_flask-2026.10.9-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 52.8 kB
Release files / requests_mock_flask-2026.10.9.tar.gz
| Download URL | requests_mock_flask-2026.10.9.tar.gz |
|---|---|
| Size | 42.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
066a5ad2dccbaa6d6c0bdb3b3797e4728bf2d421b719e0557bcd848b74a3e0c3
|
|
BLAKE2b-256 checksum How to use checksums |
15c244e5e8b72338f3c44bc0fc28657de4a2cb092eb25808d1f2afe7b6af4290
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 9, 2026.
Transparency logRelease files / requests_mock_flask-2026.10.9-py3-none-any.whl
| Download URL | requests_mock_flask-2026.10.9-py3-none-any.whl |
|---|---|
| Size | 9.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ad3afc7c74417d5051f33911f131b2bf6a03fdb7a55d696cf0bcb0ed1be272ef
|
|
BLAKE2b-256 checksum How to use checksums |
4f055720450436bb173b23e86b62c4044c7d9b565a5e433437a0b6a02f15d93f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 9, 2026.
Transparency log