requests-mock-flask


Namerequests-mock-flask JSON
Version 2023.5.14 PyPI version JSON
download
home_page
SummaryHelpers to use requests_mock and responses with a Flask test client.
upload_time2023-05-14 14:01:33
maintainer
docs_urlNone
author
requires_python>=3.10
licenseMIT License Copyright (c) 2020 Adam Dangoor Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords requests mock flask
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            |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 3.11+.

.. code:: sh

   pip install requests-mock-flask


Usage example
-------------

.. code:: python

   import flask
   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 '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!'

.. -> test_src

.. invisible-code-block: python

   import pathlib
   import subprocess
   import tempfile

   import pytest

   with tempfile.TemporaryDirectory() as tmp_dir:
       test_file = pathlib.Path(tmp_dir) / 'test_src.py'
       test_file.write_text(test_src)
       subprocess.check_output(["python", "-m", "pytest", test_file, "--basetemp", test_file.parent])


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/workflows/CI/badge.svg
   :target: https://github.com/adamtheturtle/requests-mock-flask/actions
.. |codecov| image:: https://codecov.io/gh/adamtheturtle/requests-mock-flask/branch/master/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

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "requests-mock-flask",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "",
    "keywords": "requests,mock,flask",
    "author": "",
    "author_email": "Adam Dangoor <adamdangoor@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/b7/a7/4117fac383a1f59eba0ea19d809abec6f515b4edbb46b2df5f7a4e4e9881/requests-mock-flask-2023.5.14.tar.gz",
    "platform": null,
    "description": "|Build Status| |codecov| |PyPI| |Documentation Status|\n\nrequests-mock-flask\n===================\n\n``requests-mock-flask`` helps with testing `Flask`_ applications with `httpretty`_, `responses`_ or `requests-mock`_.\n\n.. contents::\n   :local:\n\nInstallation\n------------\n\nRequires Python 3.11+.\n\n.. code:: sh\n\n   pip install requests-mock-flask\n\n\nUsage example\n-------------\n\n.. code:: python\n\n   import flask\n   import requests\n   import responses\n   import requests_mock\n\n   from requests_mock_flask import add_flask_app_to_mock\n\n   app = flask.Flask(\"test_app\")\n\n   @app.route('/')\n   def _() -> str:\n       return 'Hello, World!'\n\n   @responses.activate\n   def test_responses_decorator() -> None:\n       \"\"\"\n       It is possible to use the helper with a ``responses`` decorator.\n       \"\"\"\n       add_flask_app_to_mock(\n           mock_obj=responses,\n           flask_app=app,\n           base_url='http://www.example.com',\n       )\n\n       response = requests.get('http://www.example.com')\n\n       assert response.status_code == 200\n       assert response.text == 'Hello, World!'\n\n   def test_responses_context_manager() -> None:\n       \"\"\"\n       It is possible to use the helper with a ``responses`` context manager.\n       \"\"\"\n       with responses.RequestsMock(\n           assert_all_requests_are_fired=False,\n       ) as resp_m:\n           add_flask_app_to_mock(\n               mock_obj=resp_m,\n               flask_app=app,\n               base_url='http://www.example.com',\n           )\n\n           response = requests.get('http://www.example.com')\n\n       assert response.status_code == 200\n       assert response.text == 'Hello, World!'\n\n   def test_requests_mock_context_manager() -> None:\n       \"\"\"\n       It is possible to use the helper with a ``requests_mock`` context\n       manager.\n       \"\"\"\n       with requests_mock.Mocker() as resp_m:\n           add_flask_app_to_mock(\n               mock_obj=resp_m,\n               flask_app=app,\n               base_url='http://www.example.com',\n           )\n\n           response = requests.get('http://www.example.com')\n\n       assert response.status_code == 200\n       assert response.text == 'Hello, World!'\n\n   def test_requests_mock_adapter() -> None:\n       \"\"\"\n       It is possible to use the helper with a ``requests_mock`` fixture.\n       \"\"\"\n       session = requests.Session()\n       adapter = requests_mock.Adapter()\n       session.mount('mock', adapter)\n\n       add_flask_app_to_mock(\n           mock_obj=adapter,\n           flask_app=app,\n           base_url='mock://www.example.com',\n       )\n\n       response = session.get('mock://www.example.com')\n\n       assert response.status_code == 200\n       assert response.text == 'Hello, World!'\n\n.. -> test_src\n\n.. invisible-code-block: python\n\n   import pathlib\n   import subprocess\n   import tempfile\n\n   import pytest\n\n   with tempfile.TemporaryDirectory() as tmp_dir:\n       test_file = pathlib.Path(tmp_dir) / 'test_src.py'\n       test_file.write_text(test_src)\n       subprocess.check_output([\"python\", \"-m\", \"pytest\", test_file, \"--basetemp\", test_file.parent])\n\n\nUse cases\n---------\n\n* Use ``requests`` or other Python APIs for testing Flask applications.\n* Create a test suite which can test a Flask application as well as a live web application, to make a verified fake.\n* Test a service which calls a Flask application that you have the source code for.\n\n\nFull documentation\n------------------\n\nSee the `full documentation <https://requests-mock-flask.readthedocs.io/en/latest>`__ for more information including how to contribute.\n\n.. _Flask: https://flask.palletsprojects.com/\n.. _requests-mock: https://requests-mock.readthedocs.io/en/latest/\n.. _responses: https://github.com/getsentry/responses\n.. _httpretty: https://httpretty.readthedocs.io\n\n.. |Build Status| image:: https://github.com/adamtheturtle/requests-mock-flask/workflows/CI/badge.svg\n   :target: https://github.com/adamtheturtle/requests-mock-flask/actions\n.. |codecov| image:: https://codecov.io/gh/adamtheturtle/requests-mock-flask/branch/master/graph/badge.svg\n   :target: https://codecov.io/gh/adamtheturtle/requests-mock-flask\n.. |Documentation Status| image:: https://readthedocs.org/projects/requests-mock-flask/badge/?version=latest\n   :target: https://requests-mock-flask.readthedocs.io/en/latest/?badge=latest\n   :alt: Documentation Status\n.. |PyPI| image:: https://badge.fury.io/py/requests-mock-flask.svg\n   :target: https://badge.fury.io/py/requests-mock-flask\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2020 Adam Dangoor  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Helpers to use requests_mock and responses with a Flask test client.",
    "version": "2023.5.14",
    "project_urls": {
        "Source": "https://github.com/adamtheturtle/requests-mock-flask"
    },
    "split_keywords": [
        "requests",
        "mock",
        "flask"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2c7cbd5c740ed2507f4b4c51ba4b992aab295acac1eb03370cf3c48360babef1",
                "md5": "5b7fb6602294ac5594f07343df9961ac",
                "sha256": "5bff3a6c2e40bf628058653d552baeeadd5de7c37f3b70750b2aa92eb1e9cfaa"
            },
            "downloads": -1,
            "filename": "requests_mock_flask-2023.5.14-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5b7fb6602294ac5594f07343df9961ac",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=3.10",
            "size": 7376,
            "upload_time": "2023-05-14T14:01:32",
            "upload_time_iso_8601": "2023-05-14T14:01:32.003855Z",
            "url": "https://files.pythonhosted.org/packages/2c/7c/bd5c740ed2507f4b4c51ba4b992aab295acac1eb03370cf3c48360babef1/requests_mock_flask-2023.5.14-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b7a74117fac383a1f59eba0ea19d809abec6f515b4edbb46b2df5f7a4e4e9881",
                "md5": "1b349f08e0dd5f400a50eda945e5e4fc",
                "sha256": "0e68cad05019acd51e9bed3420221f44d41e3f285ef0be3fab26543c71c50bf0"
            },
            "downloads": -1,
            "filename": "requests-mock-flask-2023.5.14.tar.gz",
            "has_sig": false,
            "md5_digest": "1b349f08e0dd5f400a50eda945e5e4fc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 20767,
            "upload_time": "2023-05-14T14:01:33",
            "upload_time_iso_8601": "2023-05-14T14:01:33.769198Z",
            "url": "https://files.pythonhosted.org/packages/b7/a7/4117fac383a1f59eba0ea19d809abec6f515b4edbb46b2df5f7a4e4e9881/requests-mock-flask-2023.5.14.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-14 14:01:33",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "adamtheturtle",
    "github_project": "requests-mock-flask",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "requests-mock-flask"
}
        
Elapsed time: 0.06372s