===============================
requests-mock
===============================
.. image:: https://badge.fury.io/py/requests-mock.png
:target: https://pypi.org/project/requests-mock/
Intro
=====
`requests-mock` provides a building block to stub out the HTTP `requests`_ portions of your testing code.
You should checkout the `docs`_ for more information.
The Basics
==========
Everything in `requests`_ eventually goes through an adapter to do the transport work.
`requests-mock` creates a custom `adapter` that allows you to predefine responses when certain URIs are called.
There are then a number of methods provided to get the adapter used.
A simple example:
.. code:: python
>>> import requests
>>> import requests_mock
>>> session = requests.Session()
>>> adapter = requests_mock.Adapter()
>>> session.mount('mock://', adapter)
>>> adapter.register_uri('GET', 'mock://test.com', text='data')
>>> resp = session.get('mock://test.com')
>>> resp.status_code, resp.text
(200, 'data')
Obviously having all URLs be `mock://` prefixed isn't going to be useful,
so you can use `requests_mock.Mocker` to get the adapter into place.
As a context manager:
.. code:: python
>>> with requests_mock.Mocker() as m:
... m.get('http://test.com', text='data')
... requests.get('http://test.com').text
...
'data'
Or as a decorator:
.. code:: python
>>> @requests_mock.Mocker()
... def test_func(m):
... m.get('http://test.com', text='data')
... return requests.get('http://test.com').text
...
>>> test_func()
'data'
Or as a pytest fixture:
.. code:: python
>>> def test_simple(requests_mock):
... requests_mock.get('http://test.com', text='data')
... assert 'data' == requests.get('http://test.com').text
For more information checkout the `docs`_.
Reporting Bugs
==============
Development and bug tracking is performed on `GitHub`_.
Questions
=========
There is a tag dedicated to `requests-mock` on `StackOverflow`_ where you can ask usage questions.
License
=======
Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License.
.. _requests: https://requests.readthedocs.io
.. _docs: https://requests-mock.readthedocs.io/
.. _GitHub: https://github.com/jamielennox/requests-mock
.. _StackOverflow: https://stackoverflow.com/questions/tagged/requests-mock
Raw data
{
"_id": null,
"home_page": "https://requests-mock.readthedocs.io/",
"name": "requests-mock",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.5",
"maintainer_email": null,
"keywords": null,
"author": "Jamie Lennox",
"author_email": "jamielennox@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/92/32/587625f91f9a0a3d84688bf9cfc4b2480a7e8ec327cefd0ff2ac891fd2cf/requests-mock-1.12.1.tar.gz",
"platform": null,
"description": "===============================\nrequests-mock\n===============================\n\n.. image:: https://badge.fury.io/py/requests-mock.png\n :target: https://pypi.org/project/requests-mock/\n\nIntro\n=====\n\n`requests-mock` provides a building block to stub out the HTTP `requests`_ portions of your testing code.\nYou should checkout the `docs`_ for more information.\n\nThe Basics\n==========\n\nEverything in `requests`_ eventually goes through an adapter to do the transport work.\n`requests-mock` creates a custom `adapter` that allows you to predefine responses when certain URIs are called.\n\nThere are then a number of methods provided to get the adapter used.\n\nA simple example:\n\n.. code:: python\n\n >>> import requests\n >>> import requests_mock\n\n >>> session = requests.Session()\n >>> adapter = requests_mock.Adapter()\n >>> session.mount('mock://', adapter)\n\n >>> adapter.register_uri('GET', 'mock://test.com', text='data')\n >>> resp = session.get('mock://test.com')\n >>> resp.status_code, resp.text\n (200, 'data')\n\nObviously having all URLs be `mock://` prefixed isn't going to be useful,\nso you can use `requests_mock.Mocker` to get the adapter into place.\n\nAs a context manager:\n\n.. code:: python\n\n >>> with requests_mock.Mocker() as m:\n ... m.get('http://test.com', text='data')\n ... requests.get('http://test.com').text\n ...\n 'data'\n\nOr as a decorator:\n\n.. code:: python\n\n >>> @requests_mock.Mocker()\n ... def test_func(m):\n ... m.get('http://test.com', text='data')\n ... return requests.get('http://test.com').text\n ...\n >>> test_func()\n 'data'\n\nOr as a pytest fixture:\n\n.. code:: python\n\n >>> def test_simple(requests_mock):\n ... requests_mock.get('http://test.com', text='data')\n ... assert 'data' == requests.get('http://test.com').text\n\nFor more information checkout the `docs`_.\n\nReporting Bugs\n==============\n\nDevelopment and bug tracking is performed on `GitHub`_.\n\nQuestions\n=========\n\nThere is a tag dedicated to `requests-mock` on `StackOverflow`_ where you can ask usage questions.\n\nLicense\n=======\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may\nnot use this file except in compliance with the License. You may obtain\na copy of the License at\n\n https://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\nWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\nLicense for the specific language governing permissions and limitations\nunder the License.\n\n.. _requests: https://requests.readthedocs.io\n.. _docs: https://requests-mock.readthedocs.io/\n.. _GitHub: https://github.com/jamielennox/requests-mock\n.. _StackOverflow: https://stackoverflow.com/questions/tagged/requests-mock\n",
"bugtrack_url": null,
"license": "Apache-2",
"summary": "Mock out responses from the requests package",
"version": "1.12.1",
"project_urls": {
"Homepage": "https://requests-mock.readthedocs.io/",
"Source": "https://github.com/jamielennox/requests-mock"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "97ec889fbc557727da0c34a33850950310240f2040f3b1955175fdb2b36a8910",
"md5": "791372ec74430c20822fc42813dee018",
"sha256": "b1e37054004cdd5e56c84454cc7df12b25f90f382159087f4b6915aaeef39563"
},
"downloads": -1,
"filename": "requests_mock-1.12.1-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "791372ec74430c20822fc42813dee018",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": ">=3.5",
"size": 27695,
"upload_time": "2024-03-29T03:54:27",
"upload_time_iso_8601": "2024-03-29T03:54:27.640069Z",
"url": "https://files.pythonhosted.org/packages/97/ec/889fbc557727da0c34a33850950310240f2040f3b1955175fdb2b36a8910/requests_mock-1.12.1-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9232587625f91f9a0a3d84688bf9cfc4b2480a7e8ec327cefd0ff2ac891fd2cf",
"md5": "84a910e415291bf1d986aa50c0c3eedb",
"sha256": "e9e12e333b525156e82a3c852f22016b9158220d2f47454de9cae8a77d371401"
},
"downloads": -1,
"filename": "requests-mock-1.12.1.tar.gz",
"has_sig": false,
"md5_digest": "84a910e415291bf1d986aa50c0c3eedb",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.5",
"size": 60901,
"upload_time": "2024-03-29T03:54:29",
"upload_time_iso_8601": "2024-03-29T03:54:29.446559Z",
"url": "https://files.pythonhosted.org/packages/92/32/587625f91f9a0a3d84688bf9cfc4b2480a7e8ec327cefd0ff2ac891fd2cf/requests-mock-1.12.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-03-29 03:54:29",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "jamielennox",
"github_project": "requests-mock",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"requirements": [
{
"name": "requests",
"specs": [
[
"<",
"3"
],
[
">=",
"2.22"
]
]
}
],
"tox": true,
"lcname": "requests-mock"
}