requests-mock/doc/source/fixture.rst
Jamie Lennox 9fbd45b35f Fix docs building
As part of review [1] the openstack build system assumes the layout of
the docs directories to be doc/build. This is not what requests-mock was
producing. Fix the docs building directories to make it work with the
build system again.

[1] Icb0c02dc5b6f7b5e248e0df6d6093c29535b08f3

Closes-Bug: #1630114
Change-Id: Iea93ecfb0506d5ccd0b79ad35f5677797fadf730
2016-10-04 16:34:16 +11:00

43 lines
1.4 KiB
ReStructuredText

========
Fixtures
========
`Fixtures`_ provide a way to create reusable state and helper methods in test cases.
To use the *requests-mock* fixture your tests need to have a dependency on the `fixtures`_ library.
This can be optionally installed when you install *requests-mock* by doing:
.. code:: shell
pip install requests-mock[fixture]
The fixture mocks the :py:meth:`requests.Session.get_adapter` method so that all requests will be served by the mock adapter.
The fixture provides the same interfaces as the adapter.
.. doctest::
>>> import requests
>>> from requests_mock.contrib import fixture
>>> import testtools
>>> class MyTestCase(testtools.TestCase):
...
... TEST_URL = 'http://www.google.com'
...
... def setUp(self):
... super(MyTestCase, self).setUp()
... self.requests_mock = self.useFixture(fixture.Fixture())
... self.requests_mock.register_uri('GET', self.TEST_URL, text='respA')
...
... def test_method(self):
... self.requests_mock.register_uri('POST', self.TEST_URL, text='respB')
... resp = requests.get(self.TEST_URL)
... self.assertEqual('respA', resp.text)
... self.assertEqual(self.TEST_URL, self.requests_mock.last_request.url)
...
.. _Fixtures: https://pypi.python.org/pypi/fixtures
.. _mock: https://pypi.python.org/pypi/mock