Skip to main content

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

Project description

Build Status codecov PyPI Documentation Status

requests-mock-flask

requests-mock-flask helps with testing Flask applications with httpretty, responses or requests-mock.

Installation

Requires Python 3.12+.

pip install requests-mock-flask

Usage example

"""
Examples of using requests-mock-flask with responses, requests-mock
and httpretty.
"""

from http import HTTPStatus

import flask
import httpretty  # pyright: ignore[reportMissingTypeStubs]
import requests
import requests_mock
import responses

from requests_mock_flask import add_flask_app_to_mock

app = flask.Flask(import_name="test_app")


@app.route(rule="/")
def _() -> str:
    """Return a simple message."""
    return "Hello, World!"


@responses.activate
def test_responses_decorator() -> None:
    """
    It is possible to use the helper with a ``responses`` decorator.
    """
    add_flask_app_to_mock(
        mock_obj=responses,
        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!"


def test_responses_context_manager() -> None:
    """
    It is possible to use the helper with a ``responses`` context manager.
    """
    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!"


def test_requests_mock_context_manager() -> None:
    """
    It is possible to use the helper with a ``requests_mock`` context
    manager.
    """
    with requests_mock.Mocker() 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!"


def test_requests_mock_adapter() -> None:
    """
    It is possible to use the helper with a ``requests_mock`` fixture.
    """
    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!"


def test_httpretty_context_manager() -> None:
    """
    It is possible to use the helper with a ``httpretty`` context
    manager.
    """
    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!"

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-2025.1.13.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

requests_mock_flask-2025.1.13-py2.py3-none-any.whl (7.2 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: requests_mock_flask-2025.1.13.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for requests_mock_flask-2025.1.13.tar.gz
Algorithm Hash digest
SHA256 9b12e6bfa0f45401eccf4d6d264a9227f3119cd871f0ccc24780f634528e24c0
MD5 7c50a9906b4c58b1a0f05a0dc605f57c
BLAKE2b-256 d724ed85c1bc7fcd6add534e7dfad24ae92b2f4d0165bac5f82571353487a6b5

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for requests_mock_flask-2025.1.13-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 66798d5d18d6d095b3a5c23827e97dcbb56ea618c3e9d34298074418cec7698a
MD5 ff72bcfa6418034aaa83be9cc73a96b9
BLAKE2b-256 29c092ce26d5fe6041605a8d56b3004507de8b3ee1e7858bdd09d300ce104417

See more details on using hashes here.

Provenance

The following attestation bundles were made for requests_mock_flask-2025.1.13-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 Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page