README.rst 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ===============================
  2. requests-mock
  3. ===============================
  4. .. image:: https://badge.fury.io/py/requests-mock.png
  5. :target: https://pypi.org/project/requests-mock/
  6. Intro
  7. =====
  8. `requests-mock` provides a building block to stub out the HTTP `requests`_ portions of your testing code.
  9. You should checkout the `docs`_ for more information.
  10. The Basics
  11. ==========
  12. Everything in `requests`_ eventually goes through an adapter to do the transport work.
  13. `requests-mock` creates a custom `adapter` that allows you to predefine responses when certain URIs are called.
  14. There are then a number of methods provided to get the adapter used.
  15. A simple example:
  16. .. code:: python
  17. >>> import requests
  18. >>> import requests_mock
  19. >>> session = requests.Session()
  20. >>> adapter = requests_mock.Adapter()
  21. >>> session.mount('mock://', adapter)
  22. >>> adapter.register_uri('GET', 'mock://test.com', text='data')
  23. >>> resp = session.get('mock://test.com')
  24. >>> resp.status_code, resp.text
  25. (200, 'data')
  26. Obviously having all URLs be `mock://` prefixed isn't going to be useful,
  27. so you can use `requests_mock.Mocker` to get the adapter into place.
  28. As a context manager:
  29. .. code:: python
  30. >>> with requests_mock.Mocker() as m:
  31. ... m.get('http://test.com', text='data')
  32. ... requests.get('http://test.com').text
  33. ...
  34. 'data'
  35. Or as a decorator:
  36. .. code:: python
  37. >>> @requests_mock.Mocker()
  38. ... def test_func(m):
  39. ... m.get('http://test.com', text='data')
  40. ... return requests.get('http://test.com').text
  41. ...
  42. >>> test_func()
  43. 'data'
  44. Or as a pytest fixture:
  45. .. code:: python
  46. >>> def test_simple(requests_mock):
  47. ... requests_mock.get('http://test.com', text='data')
  48. ... assert 'data' == requests.get('http://test.com').text
  49. For more information checkout the `docs`_.
  50. Reporting Bugs
  51. ==============
  52. Development and bug tracking is performed on `GitHub`_.
  53. Questions
  54. =========
  55. There is a tag dedicated to `requests-mock` on `StackOverflow`_ where you can ask usage questions.
  56. License
  57. =======
  58. Licensed under the Apache License, Version 2.0 (the "License"); you may
  59. not use this file except in compliance with the License. You may obtain
  60. a copy of the License at
  61. https://www.apache.org/licenses/LICENSE-2.0
  62. Unless required by applicable law or agreed to in writing, software
  63. distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  64. WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  65. License for the specific language governing permissions and limitations
  66. under the License.
  67. .. _requests: https://requests.readthedocs.io
  68. .. _docs: https://requests-mock.readthedocs.io/
  69. .. _GitHub: https://github.com/jamielennox/requests-mock
  70. .. _StackOverflow: https://stackoverflow.com/questions/tagged/requests-mock