All Projects → idlesign → Pytest Responsemock

idlesign / Pytest Responsemock

Licence: bsd-3-clause
Simplified requests calls mocking for pytest

Programming Languages

python
139335 projects - #7 most used programming language
python3
1442 projects

Projects that are alternatives of or similar to Pytest Responsemock

elixir mock
Creates clean, concurrent, inspectable mocks from elixir modules
Stars: ✭ 21 (-12.5%)
Mutual labels:  mock, mocking
Pook
HTTP traffic mocking and testing made simple in Python
Stars: ✭ 257 (+970.83%)
Mutual labels:  mock, mocking
Retromock
Java library for mocking responses in a Retrofit service.
Stars: ✭ 48 (+100%)
Mutual labels:  mock, mocking
ineeda
Mocking library for TypeScript and JavaScript using Proxies!
Stars: ✭ 53 (+120.83%)
Mutual labels:  mock, mocking
Httmock
A mocking library for requests
Stars: ✭ 421 (+1654.17%)
Mutual labels:  requests, mock
mountebank-api-php
Working with mountebank api it's easy!
Stars: ✭ 17 (-29.17%)
Mutual labels:  mock, mocking
Hippolyte
HTTP Stubbing in Swift
Stars: ✭ 109 (+354.17%)
Mutual labels:  mock, mocking
Mockaco
🐵 HTTP mock server, useful to stub services and simulate dynamic API responses, leveraging ASP.NET Core features, built-in fake data generation and pure C# scripting
Stars: ✭ 213 (+787.5%)
Mutual labels:  mock, mocking
Mimic
Seamless client side mocking
Stars: ✭ 380 (+1483.33%)
Mutual labels:  mock, mocking
Mockolo
Efficient Mock Generator for Swift
Stars: ✭ 327 (+1262.5%)
Mutual labels:  mock, mocking
better-mock
Forked from Mockjs, Generate random data & Intercept ajax request. Support miniprogram.
Stars: ✭ 140 (+483.33%)
Mutual labels:  mock, mocking
Kakapo.js
🐦 Next generation mocking framework in Javascript
Stars: ✭ 535 (+2129.17%)
Mutual labels:  mock, mocking
umock-c
A pure C mocking library
Stars: ✭ 29 (+20.83%)
Mutual labels:  mock, mocking
mswjs.io
Official website and documentation for the Mock Service Worker library.
Stars: ✭ 77 (+220.83%)
Mutual labels:  mock, mocking
CNeptune
CNeptune improve productivity & efficiency by urbanize .net module with meta-code to lay foundation for frameworks
Stars: ✭ 30 (+25%)
Mutual labels:  mock, mocking
automock
A library for testing classes with auto mocking capabilities using jest-mock-extended
Stars: ✭ 26 (+8.33%)
Mutual labels:  mock, mocking
laika
Log, test, intercept and modify Apollo Client's operations
Stars: ✭ 99 (+312.5%)
Mutual labels:  mock, mocking
open-api-mocker
A mock server based in OpenAPI Specification
Stars: ✭ 58 (+141.67%)
Mutual labels:  mock, mocking
Firebase Mock
Firebase mock library for writing unit tests
Stars: ✭ 319 (+1229.17%)
Mutual labels:  mock, mocking
Ohhttpstubs
Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!
Stars: ✭ 4,831 (+20029.17%)
Mutual labels:  mock, mocking

pytest-responsemock

https://github.com/idlesign/pytest-responsemock

|release| |lic| |ci| |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

.. |ci| image:: https://img.shields.io/travis/idlesign/pytest-responsemock/master.svg :target: https://travis-ci.org/idlesign/pytest-responsemock

.. |coverage| image:: https://img.shields.io/coveralls/idlesign/pytest-responsemock/master.svg :target: https://coveralls.io/r/idlesign/pytest-responsemock

Description

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.

Requirements

  • Python 3.6+

Usage

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'

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')
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].