Skip to main content

Simplified requests calls mocking for pytest

Project description

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

release lic ci coverage

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 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.

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.
    # 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'

Test json response:

response = json.dumps({'key': 'value', 'another': 'yes'})

with response_mock(f'POST http://some.domain -> 400 :{response}'):
    ...

Access underlying RequestsMock (from responses package) as mock:

with response_mock('HEAD http://some.domain -> 200 :Nope') as mock:

    mock.add_passthru('http://other.domain')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-responsemock-0.1.1.tar.gz (5.2 kB view hashes)

Uploaded Source

Built Distribution

pytest_responsemock-0.1.1-py2.py3-none-any.whl (5.9 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page