A thin layer of sugar atop Python's mock.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Donald Mellenbruch 65ab497ff7
Merge branch 'dev'
3 months ago
config build: apply project template 3 months ago
docs init 3 months ago
src/mockish doc: doc all the things 3 months ago
tests init 3 months ago
.copier-answers.yml build: apply project template 3 months ago
.dockerignore init 3 months ago
.drone.yml doc: doc all the things 3 months ago
.env init 3 months ago
.gitignore init 3 months ago
.python-base-version init 3 months ago
CHANGELOG.md doc: doc all the things 3 months ago
Dockerfile-docs init 3 months ago
LICENSE build: apply project template 3 months ago
MANIFEST.in init 3 months ago
Mykefile build: apply project template 3 months ago
README.md build: apply project template 3 months ago
VERSION bump: 0.1.0 3 months ago
docker-compose.yml init 3 months ago
images.txt init 3 months ago
ruff.toml doc: doc all the things 3 months ago
setup.cfg build: apply project template 3 months ago
setup.py init 3 months ago

README.md

mockish

A thin layer of sugar atop Python's mock.

Links
Code Repo https://www.github.com/fresh2dev/mockish
Mirror Repo https://www.Fresh2.dev/code/r/mockish
Documentation https://www.Fresh2.dev/code/r/mockish/i
Changelog https://www.Fresh2.dev/code/r/mockish/i/changelog
License https://www.Fresh2.dev/code/r/mockish/i/license
Funding https://www.Fresh2.dev/funding

GitHub release (latest SemVer) GitHub Release Date License GitHub issues GitHub pull requests GitHub Repo stars PyPI - Downloads Docs Website Coverage Website Funding

Brought to you by...


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