or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdmock-objects.mdpatching.mdspecial-objects.mdutilities.md
tile.json

tessl/pypi-mock

Rolling backport of unittest.mock for all Pythons

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/mock@5.2.x

To install, run

npx @tessl/cli install tessl/pypi-mock@5.2.0

index.mddocs/

Mock

Mock is a library for testing in Python that allows you to replace parts of your system under test with mock objects and make assertions about how they have been used. This package contains a rolling backport of the standard library unittest.mock code compatible with Python 3.6 and up.

Package Information

  • Package Name: mock
  • Package Type: pypi
  • Language: Python
  • Installation: pip install mock

Core Imports

import mock

Common imports for testing:

from mock import Mock, MagicMock, patch, call, ANY, sentinel

For async testing:

from mock import AsyncMock

Basic Usage

from mock import Mock, patch, call

# Create a simple mock object
mock_obj = Mock()
mock_obj.method.return_value = 'test result'

# Call the method and assert
result = mock_obj.method('arg')
assert result == 'test result'
mock_obj.method.assert_called_with('arg')

# Using patch to replace objects temporarily
with patch('module.function') as mock_func:
    mock_func.return_value = 42
    # Code that calls module.function() will get 42
    
# Using decorators for patching
@patch('module.function')
def test_something(mock_func):
    mock_func.return_value = 'mocked'
    # Test code here

Architecture

The mock library provides a hierarchical set of mock objects and patching utilities:

  • Mock Objects: Core mock classes (Mock, MagicMock, AsyncMock) that can replace any object
  • Non-callable Mocks: Specialized mocks for non-callable objects (NonCallableMock, NonCallableMagicMock)
  • Patching System: Context managers and decorators for temporarily replacing objects
  • Assertion Helpers: Built-in methods for validating mock interactions
  • Special Objects: Sentinel values and matching utilities (ANY, call, sentinel)

This design enables comprehensive test isolation, dependency injection, and behavior verification across synchronous and asynchronous Python code.

Capabilities

Core Mock Classes

Mock object creation and configuration, including callable mocks, non-callable mocks, magic method support, and async/await compatibility.

class Mock: ...
class NonCallableMock: ...
class MagicMock(Mock): ...
class NonCallableMagicMock(NonCallableMock): ...
class AsyncMock(Mock): ...
class ThreadingMock(Mock): ...
class PropertyMock(Mock): ...

Mock Objects

Patching Functions

Temporary replacement of objects with mocks using context managers and decorators, supporting function patching, object attribute patching, and dictionary patching.

def patch(target, **kwargs): ...
def patch.object(target, attribute, **kwargs): ...
def patch.multiple(target, **kwargs): ...
class patch.dict: ...

Patching

Utility Functions

Helper functions for creating specialized mocks and managing mock behavior.

def create_autospec(spec, **kwargs): ...
def mock_open(mock=None, read_data=''): ...
def seal(mock): ...

Utilities

Special Objects and Constants

Special matching objects and sentinel values for flexible mock assertions and unique object creation.

class _ANY: ...
class _Call: ...
class _Sentinel: ...
class InvalidSpecError(Exception): ...
DEFAULT: object
sentinel: _Sentinel
ANY: _ANY
call: _Call
FILTER_DIR: bool

Special Objects