Skip to main content

Build Status PyPI

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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

requests_mock_flask-2026.9.8.tar.gz (39.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

requests_mock_flask-2026.9.8-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file requests_mock_flask-2026.9.8.tar.gz.

File metadata

  • Download URL: requests_mock_flask-2026.9.8.tar.gz
  • Upload date:
  • Size: 39.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for requests_mock_flask-2026.9.8.tar.gz
Algorithm Hash digest
SHA256 26bbf3d4c3970e2a0a78520a292092becf997e0bf7a51efbe96b8c5bbae44dec
MD5 3d8e1a1e3154d262ac8c7a2cc6ff1611
BLAKE2b-256 d3b9f44bd96b0ea81b6fee8c54d131320aba9f83431ba98eba4de0145d3655e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for requests_mock_flask-2026.9.8.tar.gz:

Publisher: release.yml on adamtheturtle/requests-mock-flask

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file requests_mock_flask-2026.9.8-py3-none-any.whl.

File metadata

File hashes

Hashes for requests_mock_flask-2026.9.8-py3-none-any.whl
Algorithm Hash digest
SHA256 3659c6744e934e5257f0d9389bd69fc3ab28fdcde7f5301ee5a7451e28f0ac73
MD5 94b485f36f1426281374d96a48faba8b
BLAKE2b-256 e7da3f0f20a76fc8fe1472f84f8726712a8a75c2780ba2dcaf3577ccc8ccb1b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for requests_mock_flask-2026.9.8-py3-none-any.whl:

Publisher: release.yml on adamtheturtle/requests-mock-flask

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

2026.9.8 This release

2 files

2026.8.16

2 files

2026.4.2

2 files

2026.2.16

2 files

2026.1.12

2 files

2025.1.13

2 files

2024.8.30.1

2 files

2024.8.30

2 files

2023.5.14

2 files

2023.3.5.1

2 files

2023.3.5

2 files

2022.4.3

2 files

2021.12.28.1

2 files

2021.12.28

2 files

2021.12.13

2 files

2021.7.10.0

2 files

2020.9.25.0

2 files

2020.9.18.0

2 files

2020.9.16.0

2 files

2020.9.14.1

2 files

2020.1.20.2

2 files

0.1

2 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