Clone wiki

macaque / Home

Macaque mocking library for Python (currently in experimental stage)

Macaque is a mocking library (or more precisely a test spy library) for Python, inspired by spock, mockito and jMock. Macaque can be used for testing the messages (method calls) between objects, and supporting the TDD/BDD process.

Macaque focuses on ease of use and being simple to read and understand. Macaque mocks (=test spy) record invocations for later inspection, and return values to the caller.

There are 2 operators which help you to keep your test clean and simple. The ">>" is used for stubbing, and "*" for verification. The former is used in the given and the latter in the expectation block.


>>> with given:
>>>    mock.some_method() >> 42

>>> print(mock.some_method())
>>> with expectation:
>>>    mock.some_method() * 3
# we are checking whether this method is called 3 times



action = mock()    
button = Button(action)

with expectation:
    action.pressed(_) *2 # called 2 times with any arg


with given:
    store = mock()    
    cache = Cache(store)
    store.load('key') >> 'value' # will return 'value'

assert cache.lookup('key') == 'value'

Complete examples