'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 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
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
Close
Hashes for requests-mock-flask-2021.7.10.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a60e85fc1e4f67b5754f932fca6130057f6cf06bffab0e6788e70eacc6fd64c7 |
|
MD5 | 0d7f8748c88c44ec96df7a28cff6cc39 |
|
BLAKE2b-256 | eb531179a2edd637b0588e3201e153c9a89e4db72108383bab157c0082e03f43 |
Close
Hashes for requests_mock_flask-2021.7.10.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3b7f57f8a2f141d99c59b9689900d6c45e1aa7f9ec2045944c2b19bc9532d28 |
|
MD5 | cf29a652fdd522c232388eb429e2e4c7 |
|
BLAKE2b-256 | 26de4fae9c05f33fe655744aa608cf318df20d1fb92ec1b9e8e3aff39b28e9cb |