Skip to main content

Helpers to use requests_mock and responses with a Flask test client.

Project description

Build Status PyPI

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


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.2.16.tar.gz (26.5 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.2.16-py2.py3-none-any.whl (6.7 kB view details)

Uploaded Python 2Python 3

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

Hashes for requests_mock_flask-2026.2.16.tar.gz
Algorithm Hash digest
SHA256 d04b5f21dab800e19701709e326b0bb8ac774b1a9e4afa51f8c29d71de6a1295
MD5 2a66b2d0a07254b02b1afd0539b5e2e5
BLAKE2b-256 fc4f6519254c16d1c17dc8a6412c13dc89e09e83564d8d582d1a823c329cc660

See more details on using hashes here.

Provenance

The following attestation bundles were made for requests_mock_flask-2026.2.16.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.2.16-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for requests_mock_flask-2026.2.16-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ff3513bef40dd91786418ca8384acdda7f653b4c05151de8f870be7394d1f6de
MD5 36f03114160b10818d48a56644612cd8
BLAKE2b-256 24071dad256e648190a3590171da4a8b6da16528ea599f4c8d9eb3d60bc2ddb7

See more details on using hashes here.

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

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

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page