A thin layer of sugar atop Python's mock.
Project description
mockish
A thin layer of sugar atop Python's mock.
Links | |
---|---|
Code Repo | https://www.github.com/fresh2dev/mockish |
Mirror Repo | https://www.f2dv.com/code/r/mockish |
Documentation | https://www.f2dv.com/code/r/mockish/i |
Changelog | https://www.f2dv.com/code/r/mockish/i/changelog |
License | https://www.f2dv.com/code/r/mockish/i/license |
Funding | https://www.f2dv.com/fund |
Overview
mockish
is a small tool I built to make life easier when writing tests in Python.
It provides:
-
Explicit alternatives to the nuanced
mock.Mock(side_effect=...)
argument, including:mockish.Mock(return_value=...)
mockish.Mock(return_call=...)
mockish.Mock(return_once=...)
mockish.Mock(return_each=...)
mockish.Mock(return_exception=...)
-
Methods for creating HTTP responses -- both
requests.Response
andhttpx.Response
objects -- that can be returned by the Mock, including:mockish.httpx.Response.from_dict(...)
mockish.requests.Response.from_dict(...)
Install
From PyPi{:target="_blank"}:
pip install mockish
Use
Complete example of mocking a HTTP response:
from mockish import Mock, patch
from mockish.requests import Response
import requests
mock_resp = Response.from_dict({'hello': 'world'})
with patch.object(
requests,
'get',
Mock(return_once=mock_resp)
):
resp: requests.Response = requests.get('https://www.fresh2.dev')
requests.get.assert_called_once()
print(resp)
> <Response [200]>
print(resp.json())
> {'hello': 'world'}
See the reference docs for more examples:
Support
If this project delivers value to you, please provide feedback, code contributions, and/or funding.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
mockish-0.1.1.tar.gz
(53.3 kB
view hashes)
Built Distribution
mockish-0.1.1-py3-none-any.whl
(43.2 kB
view hashes)