GitXplorerGitXplorer
a

requests-mock-flask

public
9 stars
0 forks
1 issues

Commits

List of commits on branch main.
Verified
12ce74b3e967ddf1443fd75aa5da10d02a88b75b

Merge pull request #1246 from adamtheturtle/dependabot/pip/pyright-1.1.392.post0

ggithub-actions[bot] committed 3 days ago
Verified
a2c672f41b614e00a5faa616a6ab9adab1256ae5

Bump pyright from 1.1.391 to 1.1.392.post0

ddependabot[bot] committed 3 days ago
Unverified
a60c4168ba790ec5db491d453761914deafd680e

Bump CHANGELOG

aadamtheturtle committed 5 days ago
Verified
fa872f149f997de90dfdc47cdf7b5bada8d94a95

Merge pull request #1244 from adamtheturtle/dependabot/pip/ruff-0.9.1

ggithub-actions[bot] committed 5 days ago
Verified
64dabc446574c1c864e596e951557a4bf1d66906

Bump ruff from 0.9.0 to 0.9.1

ddependabot[bot] committed 5 days ago
Verified
ccfebd76dbf8c7f223ca69c89b068c0b3ea3c4cc

Merge pull request #1242 from adamtheturtle/dependabot/pip/deptry-0.22.0

ggithub-actions[bot] committed 5 days ago

README

The README file for this repository.

|Build Status| |codecov| |PyPI| |Documentation Status|

requests-mock-flask

requests-mock-flask helps with testing Flask_ applications with httpretty, responses or requests-mock_.

.. contents:: :local:

Installation

Requires Python |minimum-python-version|+.

.. code-block:: sh

pip install requests-mock-flask

Usage example

.. code-block:: python

""" 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 <https://requests-mock-flask.readthedocs.io/en/latest>__ for more information including how to contribute.

.. _Flask: https://flask.palletsprojects.com/ .. _requests-mock: https://requests-mock.readthedocs.io/en/latest/ .. _responses: https://github.com/getsentry/responses .. _httpretty: https://httpretty.readthedocs.io

.. |Build Status| image:: https://github.com/adamtheturtle/requests-mock-flask/actions/workflows/ci.yml/badge.svg?branch=main :target: https://github.com/adamtheturtle/requests-mock-flask/actions .. |codecov| image:: https://codecov.io/gh/adamtheturtle/requests-mock-flask/branch/main/graph/badge.svg :target: https://codecov.io/gh/adamtheturtle/requests-mock-flask .. |Documentation Status| image:: https://readthedocs.org/projects/requests-mock-flask/badge/?version=latest :target: https://requests-mock-flask.readthedocs.io/en/latest/?badge=latest :alt: Documentation Status .. |PyPI| image:: https://badge.fury.io/py/requests-mock-flask.svg :target: https://badge.fury.io/py/requests-mock-flask .. |minimum-python-version| replace:: 3.12