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 responses or requests-mock.

Installation

Requires Python 3.9+.

pip install requests-mock-flask

Usage example

import flask
import requests
import responses
import requests_mock

from requests_mock_flask import add_flask_app_to_mock

app = flask.Flask(__name__)

@app.route('/')
def _() -> str:
    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('http://www.example.com')

    assert response.status_code == 200
    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('http://www.example.com')

    assert response.status_code == 200
    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('http://www.example.com')

    assert response.status_code == 200
    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('mock', adapter)

    add_flask_app_to_mock(
        mock_obj=adapter,
        flask_app=app,
        base_url='mock://www.example.com',
    )

    response = session.get('mock://www.example.com')

    assert response.status_code == 200
    assert response.text == 'Hello, World!'

Use cases

  • Use the requests API for 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.

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

Uploaded Source

Built Distribution

requests_mock_flask-2021.12.13-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file requests-mock-flask-2021.12.13.tar.gz.

File metadata

  • Download URL: requests-mock-flask-2021.12.13.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for requests-mock-flask-2021.12.13.tar.gz
Algorithm Hash digest
SHA256 a58af3ccdba1d185fc3ce6fab0dba287ab2f58ef6c90d76d27d267b3db325290
MD5 65626ef11afa6d7cb509771f6d67b548
BLAKE2b-256 c70c49086267ec2fa073dc845cfa2ec24faf6d592a70bba2a04b9103c834bba3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: requests_mock_flask-2021.12.13-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for requests_mock_flask-2021.12.13-py3-none-any.whl
Algorithm Hash digest
SHA256 713e8482db2a743c92c2c024934e738524b65b7ec25000d5582ad1db69192175
MD5 776accba96197d526b020feb7ef23569
BLAKE2b-256 e21d814b4b40e4a045e00a6759a7f0dc6f4dcccd7b079cd99883476bc47b2a8c

See more details on using hashes here.

Supported by

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