requests-mock-flask


Namerequests-mock-flask JSON
Version 2024.8.30.1 PyPI version JSON
download
home_pageNone
SummaryHelpers to use requests_mock and responses with a Flask test client.
upload_time2024-08-30 03:50:40
maintainerNone
docs_urlNone
authorNone
requires_python>=3.12
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 flask mock requests
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.12+.

.. code:: sh

   pip install requests-mock-flask


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

.. code:: python

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

.. -> 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/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

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "requests-mock-flask",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.12",
    "maintainer_email": null,
    "keywords": "flask, mock, requests",
    "author": null,
    "author_email": "Adam Dangoor <adamdangoor@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/c4/e2/ff0a60d1b00728c8f66ece46c9f6461cf86abde0c157d415d13cb65c02f3/requests_mock_flask-2024.8.30.1.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.12+.\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 httpretty\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 a simple message.\"\"\"\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   def test_httpretty_context_manager() -> None:\n       \"\"\"\n       It is possible to use the helper with a ``httpretty`` context\n       manager.\n       \"\"\"\n       with httpretty.core.httprettized():\n           add_flask_app_to_mock(\n               mock_obj=httpretty,\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.. -> 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/main/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": "2024.8.30.1",
    "project_urls": {
        "Source": "https://github.com/adamtheturtle/requests-mock-flask"
    },
    "split_keywords": [
        "flask",
        " mock",
        " requests"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7c429d540b63544f9556626f69b0136eaec7e15d7f77cf603c5f09d913a9720b",
                "md5": "d9dddc8103ea2e61c6d6ad6310d169de",
                "sha256": "60534ef67ea01821a41f7da4d75b1338114814bd5a0bcb5f232a597df6945638"
            },
            "downloads": -1,
            "filename": "requests_mock_flask-2024.8.30.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d9dddc8103ea2e61c6d6ad6310d169de",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=3.12",
            "size": 7116,
            "upload_time": "2024-08-30T03:50:38",
            "upload_time_iso_8601": "2024-08-30T03:50:38.412132Z",
            "url": "https://files.pythonhosted.org/packages/7c/42/9d540b63544f9556626f69b0136eaec7e15d7f77cf603c5f09d913a9720b/requests_mock_flask-2024.8.30.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c4e2ff0a60d1b00728c8f66ece46c9f6461cf86abde0c157d415d13cb65c02f3",
                "md5": "501032de4c7f46d6b3cdf7258cee9a5d",
                "sha256": "1b750b835b07a395267e5440a4fbb88caa043c57c0bb785e1687b03c127347ce"
            },
            "downloads": -1,
            "filename": "requests_mock_flask-2024.8.30.1.tar.gz",
            "has_sig": false,
            "md5_digest": "501032de4c7f46d6b3cdf7258cee9a5d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.12",
            "size": 23306,
            "upload_time": "2024-08-30T03:50:40",
            "upload_time_iso_8601": "2024-08-30T03:50:40.278526Z",
            "url": "https://files.pythonhosted.org/packages/c4/e2/ff0a60d1b00728c8f66ece46c9f6461cf86abde0c157d415d13cb65c02f3/requests_mock_flask-2024.8.30.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-30 03:50:40",
    "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: 9.88628s