Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
https://github.com/idlesign/pytest-responsemock
|release| |lic| |coverage|
.. |release| image:: https://img.shields.io/pypi/v/pytest-responsemock.svg :target: https://pypi.python.org/pypi/pytest-responsemock
.. |lic| image:: https://img.shields.io/pypi/l/pytest-responsemock.svg :target: https://pypi.python.org/pypi/pytest-responsemock
.. |coverage| image:: https://img.shields.io/coveralls/idlesign/pytest-responsemock/master.svg :target: https://coveralls.io/r/idlesign/pytest-responsemock
Simplified requests calls mocking for pytest
Provides response_mock
fixture, exposing simple context manager.
Any request under that manager will be intercepted and mocked according
to one or more rules
passed to the manager. If actual request won't fall
under any of given rules then an exception is raised (by default).
Rules are simple strings, of the pattern: HTTP_METHOD URL -> STATUS_CODE :BODY
.
When this package is installed response_mock
is available for pytest
test functions.
.. code-block:: python
def for_test():
return requests.get('http://some.domain')
def test_me(response_mock):
# Pass response rule as a string,
# or many rules (to mock consequent requests) as a list of strings/bytes.
# Use optional `bypass` argument to disable mock conditionally.
with response_mock('GET http://some.domain -> 200 :Nice', bypass=False):
result = for_test()
assert result.ok
assert result.content == b'Nice'
# mock consequent requests
with response_mock([
'GET http://some.domain -> 200 :Nice',
'GET http://other.domain -> 200 :Sweet',
]):
for_test()
requests.get('http://other.domain')
Use with pytest-datafixtures
:
.. code-block:: python
def test_me(response_mock, datafix_read):
with response_mock(f"GET http://some.domain -> 200 :{datafix_read('myresponse.html')}"):
...
Describe response header fields using multiline strings:
.. code-block:: python
with response_mock(
'''
GET http://some.domain
Allow: GET, HEAD
Content-Language: ru
-> 200 :OK
'''
):
...
Test json response:
.. code-block:: python
response = json.dumps({'key': 'value', 'another': 'yes'})
with response_mock(f'POST http://some.domain -> 400 :{response}'):
...
To test binary response pass rule as bytes:
.. code-block:: python
with response_mock(b'GET http://some.domain -> 200 :' + my_bytes):
...
Access underlying RequestsMock (from responses
package) as mock
:
.. code-block:: python
with response_mock('HEAD http://some.domain -> 200 :Nope') as mock:
mock.add_passthru('http://other.domain')
FAQs
Simplified requests calls mocking for pytest
We found that pytest-responsemock demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.