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 the
requestsfor testing 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.
Reference¶
- API Reference
- Release Process
- Unreleased changes
- [UNRELEASED DRAFT] (2026-10-12)
- Changelog
- 2026.10.09 (2026-10-09)
- 2026.09.08
- 2026.08.16
- 2026.04.02
- 2026.02.16
- 2026.01.12
- 2025.01.13
- 2024.08.30.1
- 2024.08.30
- 2023.05.14
- 2023.03.05.1
- 2023.03.05
- 2022.04.03
- 2021.12.28.1
- 2021.12.28
- 2021.12.13
- 2021.07.10.0
- 2020.09.25.0
- 2020.09.18.0
- 2020.09.16.0
- 2020.09.14.1
- 2020.09.14.0
- 2020.01.20.2
- 2020.01.20.1
- 2020.01.20.0
- Contributing to requests-mock-flask