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

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: requests_mock_flask-2026.4.2.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.4.2.tar.gz
Algorithm Hash digest
SHA256 0fada5104d187cc5ebb22c27dec367cc2174114c2152c8c57c4e0f96fd9dcb49
MD5 4982fd97199b22a0fef446e70935e9af
BLAKE2b-256 5136dab370235de8fe5404d79eab4f53e724b1e653568e4a77afb75f14c807d1

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for requests_mock_flask-2026.4.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ea88de696f4c33ef77f544fd5a05d3ac123b066e5d4fa83468293e174caefc56
MD5 c44f86fbd7e5787ae9c4b83d5b493788
BLAKE2b-256 05e72955c6b40be56786a788029f354e5d7e4c5f56b75cdd62b6a29cc4cdb5bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for requests_mock_flask-2026.4.2-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