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.11+.

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`` adapter.
    """
    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!'

@httpretty.activate
def test_httpretty_decorator() -> None:
    """
    It is possible to use the helper with HTTPretty.
    """
    add_flask_app_to_mock(
        mock_obj=httpretty,
        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!'

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

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for requests-mock-flask-2023.3.5.tar.gz
Algorithm Hash digest
SHA256 6d70531caa22912ce4dcdcac3397ce52ac042fcb70b1e55b0603936c09d657d9
MD5 b7a95e6eb82562ea6825414664f900ab
BLAKE2b-256 2a223e36a80227ad9293b6d721c9fb723cf00a7eb0b53c5f91182bfd2917129a

See more details on using hashes here.

File details

Details for the file requests_mock_flask-2023.3.5-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for requests_mock_flask-2023.3.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7a424c35285d5b91d69b9ee2104258110de926bdb541f96707ac5808505ba89e
MD5 f663a6c58b355347e8aba95e7f08d6eb
BLAKE2b-256 7e54e948593edefe237baf274edc5a2995235d496a14eeb2bcdeb7120b7d9ea2

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