Skip to main content

Easily mock calls to ubersmith at the `requests` level.

Project description

Mock out calls to the python-ubersmith library

Usage

pytest-ubersmith makes it really easy to mock out API calls:

import ubersmith.client

def test_add_client(ubermock):
    client_id = 1234
    ubermock.client.add = client_id

    assert ubersmith.client.add(login='test', password='abc') == 1234

    # Easy checking of calls
    assert ubermock.client.add.called
    assert ubermock.client.add.call_count == 1
    ubermock.client.add.assert_called_once_with(login='test')
    ubermock.client.add.assert_called_once_with_exactly(login='test',
                                                        password='abc')

To have python-ubersmith raise a ResponseError, return an ubermock.ResponseError:

import ubersmith.client
import ubersmith.exceptions

def test_unknown_client(ubermock):
    ubermock.client.get = ubermock.ResponseError('Invalid client!', 1)

    with pytest.raises(ubersmith.exceptions.ResponseError):
        ubersmith.client.get(client_id=1234)

If you need complete control over the response, you can return the whole Ubersmith JSON response:

import pytest
import ubersmith.client
import ubersmith.exceptions

def test_unknown_client_raw(ubermock):
    ubermock.client.get.raw_response = {
        'status': False,
        'data': '',
        'error_message': 'Invalid client',
        'error_code': 1,
    }

    with pytest.raises(ubersmith.exceptions.ResponseError):
        ubersmith.client.get(client_id=1234)

You can even pass a callable for dynamic responses:

import pytest
import ubersmith.client
import ubersmith.exceptions

def test_dynamic_client(ubermock):
    def get_client(method, params, request, context):
        if params['client_id'] == '1':
            return {'client_id': 1}
        else:
            raise ubermock.ResponseError('Invalid client!', 1)

    ubermock.client.get = get_client

    assert ubersmith.client.get(client_id=1) == {'client_id': 1}
    with pytest.raises(ubersmith.exceptions.ResponseError):
        ubersmith.client.get(client_id=2)

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-ubersmith-1.2.0.tar.gz (5.5 kB view details)

Uploaded Source

File details

Details for the file pytest-ubersmith-1.2.0.tar.gz.

File metadata

File hashes

Hashes for pytest-ubersmith-1.2.0.tar.gz
Algorithm Hash digest
SHA256 c5962bf0d815cccc94cc75914ca0821505b388eaa5b69b37e71b10daef9628d5
MD5 0a5b82853d083a7d0ff1351b7b6d87e0
BLAKE2b-256 a7be99142b40cf55037cac33883d8d55e7302ced556ff8f310765d8de90a1ac4

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