Welcome to Mockify!

About Mockify

Mockify is a highly customizable and expressive mocking library for Python inspired by Google Mock C++ framework, but adopted to Python world.

Unlike tools like unittest.mock, Mockify is based on expectations that you record on your mocks before they are injected to code being under test. Each expectation represents arguments the mock is expected to be called with and provides sequence of actions the mock will do when called with that arguments. Actions allow to set a value to be returned, exception to be raised or just function to be called. Alternatively, if no actions should take place, you can just say how many times the mock is expected to be called. And all of these is provided by simple, expressive and easy to use API.

Here’s a simple example:

from mockify.core import satisfied
from mockify.mock import Mock
from mockify.actions import Return

def invoke(func):
    return func()

def test_invoke_calls_func_returning_hello_world():
    func = Mock('func')
    func.expect_call().will_once(Return('Hello, world!'))

    with satisfied(func):
        assert invoke(func) == 'Hello, world!'

I hope you’ll find this library useful.

User’s Guide