Callee: Argument Matchers for unittest.mock

0.3.1 · active · verified Fri Apr 17

Callee provides a set of powerful argument matchers designed to extend the capabilities of Python's `unittest.mock` library. It allows developers to write more expressive and robust assertions for mocked calls, enabling checks for patterns, types, ranges, and more, beyond simple value equality. The current version is 0.3.1, with a stable but infrequent release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `callee` matchers with `unittest.mock` to assert more complex argument patterns. It creates a mock object, calls a function that uses it, and then uses `Eq`, `Regex`, and `Any` matchers within `assert_has_calls` to verify the arguments passed to the mock.

from unittest import mock
from callee import Eq, Regex, Any

class MyService:
    def process_data(self, data_id, name):
        # Imagine this does something with data_id and name
        return f"Processed {name} with ID {data_id}"

def external_function(service: MyService):
    # This function calls methods on the provided service
    service.process_data(123, "test_name_abc")
    service.process_data(456, "another_name_xyz")

# Create a mock for MyService
mock_service = mock.Mock(spec=MyService)

# Call the function that uses the service, injecting our mock
external_function(mock_service)

# Assert that process_data was called with specific matchers
# This checks the first call
mock_service.process_data.assert_has_calls([
    mock.call(Eq(123), Regex(r'^test_name_.*$')),
    mock.call(Any(int), Any(str)) # Any integer, any string for the second call
])

print("Assertions successful!")

view raw JSON →