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
"""
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
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-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
Algorithm | Hash digest | |
---|---|---|
SHA256 |
9b12e6bfa0f45401eccf4d6d264a9227f3119cd871f0ccc24780f634528e24c0
|
|
MD5 |
7c50a9906b4c58b1a0f05a0dc605f57c
|
|
BLAKE2b-256 |
d724ed85c1bc7fcd6add534e7dfad24ae92b2f4d0165bac5f82571353487a6b5
|
Provenance
The following attestation bundles were made for requests_mock_flask-2025.1.13.tar.gz
:
Publisher:
release.yml
on adamtheturtle/requests-mock-flask
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
requests_mock_flask-2025.1.13.tar.gz
-
Subject digest:
9b12e6bfa0f45401eccf4d6d264a9227f3119cd871f0ccc24780f634528e24c0
- Sigstore transparency entry: 162088078
- Sigstore integration time:
-
Permalink:
adamtheturtle/requests-mock-flask@fa872f149f997de90dfdc47cdf7b5bada8d94a95
-
Branch / Tag:
refs/heads/main
- Owner: https://github.com/adamtheturtle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
release.yml@fa872f149f997de90dfdc47cdf7b5bada8d94a95
-
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file requests_mock_flask-2025.1.13-py2.py3-none-any.whl
.
File metadata
- Download URL: requests_mock_flask-2025.1.13-py2.py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
66798d5d18d6d095b3a5c23827e97dcbb56ea618c3e9d34298074418cec7698a
|
|
MD5 |
ff72bcfa6418034aaa83be9cc73a96b9
|
|
BLAKE2b-256 |
29c092ce26d5fe6041605a8d56b3004507de8b3ee1e7858bdd09d300ce104417
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
requests_mock_flask-2025.1.13-py2.py3-none-any.whl
-
Subject digest:
66798d5d18d6d095b3a5c23827e97dcbb56ea618c3e9d34298074418cec7698a
- Sigstore transparency entry: 162088080
- Sigstore integration time:
-
Permalink:
adamtheturtle/requests-mock-flask@fa872f149f997de90dfdc47cdf7b5bada8d94a95
-
Branch / Tag:
refs/heads/main
- Owner: https://github.com/adamtheturtle
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
release.yml@fa872f149f997de90dfdc47cdf7b5bada8d94a95
-
Trigger Event:
workflow_dispatch
-
Statement type: