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

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:

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

Uploaded Source

Built Distribution

pytest_responsemock-1.0.1-py2.py3-none-any.whl (6.4 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: pytest-responsemock-1.0.1.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.8

File hashes

Hashes for pytest-responsemock-1.0.1.tar.gz
Algorithm Hash digest
SHA256 52652933f1019b8aaedae480ec230ba58728aa08904077fcce86725a90bd0ccc
MD5 3622774e7c10b84c708764ff0143b583
BLAKE2b-256 9de5f0a5d4728dd9f3b3111ef622771c5d56f3f6d4d2677ca120912ac45b64a2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytest_responsemock-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 96a69611af4be0d492c6de9dd4133ee6e0b2bbf338be03470f432b5ba12695a2
MD5 e11c7973fef5a600c66cf6fc4bf232ce
BLAKE2b-256 173a942710f5d6e664ad309aa24a5aaa6f55613b95c72c2beb4731517aedd129

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