Helpers to use requests_mock and responses with a Flask test client.
Project description
requests-mock-flask
requests-mock-flask helps with testing Flask applications with httpretty, respx, 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 # pyright: ignore[reportMissingTypeStubs]
import httpx
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(): # type: ignore[no-untyped-call]
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!"
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file requests_mock_flask-2026.2.16.tar.gz.
File metadata
- Download URL: requests_mock_flask-2026.2.16.tar.gz
- Upload date:
- Size: 26.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d04b5f21dab800e19701709e326b0bb8ac774b1a9e4afa51f8c29d71de6a1295
|
|
| MD5 |
2a66b2d0a07254b02b1afd0539b5e2e5
|
|
| BLAKE2b-256 |
fc4f6519254c16d1c17dc8a6412c13dc89e09e83564d8d582d1a823c329cc660
|
Provenance
The following attestation bundles were made for requests_mock_flask-2026.2.16.tar.gz:
Publisher:
release.yml on adamtheturtle/requests-mock-flask
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
requests_mock_flask-2026.2.16.tar.gz -
Subject digest:
d04b5f21dab800e19701709e326b0bb8ac774b1a9e4afa51f8c29d71de6a1295 - Sigstore transparency entry: 954383827
- Sigstore integration time:
-
Permalink:
adamtheturtle/requests-mock-flask@7e1ed9830a1e73b5c6d4a16bf5eeb47aa19fb85c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/adamtheturtle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7e1ed9830a1e73b5c6d4a16bf5eeb47aa19fb85c -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file requests_mock_flask-2026.2.16-py2.py3-none-any.whl.
File metadata
- Download URL: requests_mock_flask-2026.2.16-py2.py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff3513bef40dd91786418ca8384acdda7f653b4c05151de8f870be7394d1f6de
|
|
| MD5 |
36f03114160b10818d48a56644612cd8
|
|
| BLAKE2b-256 |
24071dad256e648190a3590171da4a8b6da16528ea599f4c8d9eb3d60bc2ddb7
|
Provenance
The following attestation bundles were made for requests_mock_flask-2026.2.16-py2.py3-none-any.whl:
Publisher:
release.yml on adamtheturtle/requests-mock-flask
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
requests_mock_flask-2026.2.16-py2.py3-none-any.whl -
Subject digest:
ff3513bef40dd91786418ca8384acdda7f653b4c05151de8f870be7394d1f6de - Sigstore transparency entry: 954383828
- Sigstore integration time:
-
Permalink:
adamtheturtle/requests-mock-flask@7e1ed9830a1e73b5c6d4a16bf5eeb47aa19fb85c -
Branch / Tag:
refs/heads/main - Owner: https://github.com/adamtheturtle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7e1ed9830a1e73b5c6d4a16bf5eeb47aa19fb85c -
Trigger Event:
workflow_dispatch
-
Statement type: