Skip to content

mockish

A thin layer of sugar atop Python's mock.



GitHub tag (with filter) GitHub last commit (branch) GitHub Repo stars

Overview

mockish is a small tool I built to make life easier when writing tests in Python.

It provides:

  1. 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=...)
  2. Methods for creating HTTP responses -- both requests.Response and httpx.Response objects -- that can be returned by the Mock, including:

    • mockish.httpx.Response.from_dict(...)
    • mockish.requests.Response.from_dict(...)

Install

From PyPi:

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.f2dv.com')

    requests.get.assert_called_once()

print(resp)
> <Response [200]>

print(resp.json())
> {'hello': 'world'}

See the reference docs for more examples:


Brought to you by...