Skip to main content

Spying framework

Project description

Mockito is a spying framework originally based on the Java library with the same name. (Actually we invented the strict stubbing mode back in 2009.)

https://github.com/kaste/mockito-python/actions/workflows/test-lint-go.yml/badge.svg

Install

pip install mockito

Quick Start

90% use case is that you want to stub out a side effect.

from mockito import when, mock, unstub

when(os.path).exists('/foo').thenReturn(True)

# or:
import requests  # the famous library
# you actually want to return a Response-like obj, we'll fake it
response = mock({'status_code': 200, 'text': 'Ok'})
when(requests).get(...).thenReturn(response)

# use it
requests.get('http://google.com/')

# clean up
unstub()

Read the docs

http://mockito-python.readthedocs.io/en/latest/

Breaking changes in v2

Two functions have been renamed:

  • verifyNoMoreInteractions is deprecated. Use ensureNoUnverifiedInteractions instead.

Although verifyNoMoreInteractions is the name used in mockito for Java, it is a misnomer over there too (imo). Its docs say “Checks if any of given mocks has any unverified interaction.”, and we make that clear now in the name of the function, so you don’t need the docs to tell you what it does.

  • verifyNoUnwantedInteractions is deprecated. Use verifyExpectedInteractions instead.

The new name should make it clear that it corresponds to the usage of expect (as alternative to when).

Context managers now check the usage and any expectations (if you used expect) on exit. You can disable this check by setting the environment variable MOCKITO_CONTEXT_MANAGERS_CHECK_USAGE to “0”. Note that this does not disable the check for any explicit expectations you might have set with expect.

This roughly corresponds to the verifyStubbedInvocationsAreUsed contra the verifyExpectedInteractions functions.

New in v2

  • between now supports open ranges, e.g. between=(0, ) to check that at least 0 interactions occurred.

Development

I use uv, and if you do too: you just clone this repo to your computer, then run uv sync in the root directory. Example usage:

uv run pytest

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

mockito-2.0.0a1.tar.gz (182.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mockito-2.0.0a1-py3-none-any.whl (34.2 kB view details)

Uploaded Python 3

File details

Details for the file mockito-2.0.0a1.tar.gz.

File metadata

  • Download URL: mockito-2.0.0a1.tar.gz
  • Upload date:
  • Size: 182.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mockito-2.0.0a1.tar.gz
Algorithm Hash digest
SHA256 6a70a5207f28e4147b1775624e9405f5fe283df1fa0f888aae141bbc95b6017a
MD5 55647e8243c0c12f76db711d9586767a
BLAKE2b-256 22c98880dda9a64d4aab16d40602855de27ab18d67eeee5b4da6510113f7ba7c

See more details on using hashes here.

File details

Details for the file mockito-2.0.0a1-py3-none-any.whl.

File metadata

  • Download URL: mockito-2.0.0a1-py3-none-any.whl
  • Upload date:
  • Size: 34.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mockito-2.0.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 8ac3db84b7a1a1fbc235c23179c6e2a7d1bc30d1d7f1a7ed02f7da41e07bd712
MD5 0e0ce26811ebd0f26a8d435ac90652ee
BLAKE2b-256 211c36d20f7d4de68e1858504a629d243bd210c7ee0a208aadb29f7a88072321

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page