Skip to main content

Simplified requests calls mocking for pytest

Project description

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

release lic 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 the pattern: HTTP_METHOD URL -> STATUS_CODE :BODY.

Requirements

  • Python 3.7+

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/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:

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:

with response_mock(
    '''
    GET http://some.domain

    Allow: GET, HEAD
    Content-Language: ru

    -> 200 :OK
    '''
):
    ...

Test json response:

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:

with response_mock(b'GET http://some.domain -> 200 :' + my_bytes):
    ...

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-1.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

pytest_responsemock-1.1.0-py2.py3-none-any.whl (6.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pytest-responsemock-1.1.0.tar.gz.

File metadata

File hashes

Hashes for pytest-responsemock-1.1.0.tar.gz
Algorithm Hash digest
SHA256 3bf6cf4c8be27871edc883ca7e7caadb8dee5a0cb4ff0865b19acd7c9a9b7550
MD5 9dab0af80d3251cdf0bc3d99714556f0
BLAKE2b-256 f27e674e43bbb90e03b1e57a4060883f6412880b1e89a3f446fcde609401af4e

See more details on using hashes here.

File details

Details for the file pytest_responsemock-1.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_responsemock-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 cca7a77dddee7f977f404f02c2ec37a80b7c661853106334f622cbe86186ce5e
MD5 b45b5b8c87974bf69593f0b6c4dbfa87
BLAKE2b-256 90e64bd3ff00d4e48d2d6293bfe70bd9f0cb3b82b8432a789fc228e990810f2a

See more details on using hashes here.

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