Helpers to use requests_mock and responses with a Flask test client.
Project description
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
import flask
import httpretty
import requests
import responses
import requests_mock
from requests_mock_flask import add_flask_app_to_mock
app = flask.Flask("test_app")
@app.route('/')
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('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!'
def test_httpretty_context_manager() -> None:
"""
It is possible to use the helper with a ``httpretty`` context
manager.
"""
with httpretty.core.httprettized():
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file requests_mock_flask-2024.8.30.tar.gz
.
File metadata
- Download URL: requests_mock_flask-2024.8.30.tar.gz
- Upload date:
- Size: 23.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cdd77d8c1be456e04939d2be51fd4520884e8f210cf32ae07af2ca22b013b71f |
|
MD5 | d3f75d0268ae8cdafb5ff62244f92f0a |
|
BLAKE2b-256 | ce987926007e441280f5b42e9b8aff4846ad731a04784f7c33d637e48ffbd9d2 |
File details
Details for the file requests_mock_flask-2024.8.30-py2.py3-none-any.whl
.
File metadata
- Download URL: requests_mock_flask-2024.8.30-py2.py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92ee9c2e5a0ac1e6d42107a79b2152de2033e24c5507fdbe69a02daee0a3dbb7 |
|
MD5 | dc2a7aa6b62bcee1af5ae7fe535bb597 |
|
BLAKE2b-256 | be205dea6314e9758323b18d285e17f0ba570c293db26dd6d5448925207c5d12 |