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') == 1234
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):
# params is a MultiDict
if params['client_id'][0] == '1':
return {'client_id': 1}
else:
raise ubermock.ResponseError('Invalid client!', 1)
ubermock.client.get.response = 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
File details
Details for the file pytest-ubersmith-1.0.2.tar.gz
.
File metadata
- Download URL: pytest-ubersmith-1.0.2.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7c5080f54df6c987d117d0ce0bed801fe0342c05e90573bd83e55457815c14d |
|
MD5 | 79aa26914c33f5b00aeea89e744ab0e7 |
|
BLAKE2b-256 | b215094617fd5233be23366f8b6e6caea9f948a3c2b2efc0d4ad59f89793b54d |