mockify.mock - Classes for mocking things

Classes for mocking things.

class mockify.mock.Object(name, methods=None, properties=None, registry=None)

Bases: object

Class for mocking Python objects.

Changed in version 0.5: New API introduced.

New in version 0.3.

Since version 0.5 this class provides a new API that complies with the one used by other mock classes.

You can now create mock objects directly, without subclassing:

mock = Object('mock')

Method calls are now recorded like this:, 2)

And for recording property get/set expectations you write:

And you can still subclass this class and provide a set of methods and properties, like in this example:

class Dummy(Object):
    __methods__ = ['foo']
    __properties__ = ['bar']
  • name – Name of mocked object
  • methods

    Sequence of names of methods to be mocked.

    If this is given, then the only allowed methods will be the ones from given sequence. Attempt to access any other will result in AttributeError being raised.

  • properties

    Sequence of names of properties to be mocked.

    Use is the same as for methods parameter.

  • registry

    Instance of mockify.Registry class.

    If not given, a default one will be created for this mock object.


Assert that all expected method/property calls are satisfied.

expect_call(__name__, *args, **kwargs)

Record method call expectation.

Deprecated since version 0.5: See Object for a brief example of how to use new API.


Record property get expectation.

Deprecated since version 0.5: See Object for a brief example of how to use new API.

expect_set(__name__, value)

Record property set expectation.

Deprecated since version 0.5: See Object for a brief example of how to use new API.

class mockify.mock.Function(name, registry=None)

Bases: object

Class for mocking Python functions.

Example usage:

>>> foo = Function('foo')
>>> foo.expect_call(1, 2).times(2)
<mockify.Expectation: foo(1, 2)>
>>> for _ in range(2):
...     foo(1, 2)
>>> foo.assert_satisfied()
  • name – Mock function name
  • registry

    This is optional.

    Use this to pass custom instance of mockify.engine.Registry class if you need to share it between multiple frontends. Sharing is useful for example to check if all mocks are satisfied using one assert_satisfied call:

    >>> from mockify import Registry
    >>> reg = Registry()
    >>> foo = Function('foo', registry=reg)
    >>> bar = Function('bar', registry=reg)
    >>> foo.expect_call()
    <mockify.Expectation: foo()>
    >>> bar.expect_call()
    <mockify.Expectation: bar()>
    >>> foo()
    >>> bar()
    >>> reg.assert_satisfied()

Assert that this function mock is satisfied.

This method just calls mockify.engine.Registry.assert_satisfied() with name given via constructor as an argument.

expect_call(*args, **kwargs)

Record call expectation.

This method creates mockify.engine.Call instance giving it args and kwargs, fetches file and line number from current call stack and triggers mockify.engine.Registry.expect_call() and returns expectation object it produces.

class mockify.mock.FunctionFactory(registry=None)

Bases: object

Helper factory class for easier function mocks creating.

This helper can be created with no params or with mockify.engine.Registry instance as parameter. It provides an easy way of function mock creating by simply getting factory attributes that become function mock names. Once such attribute is get for the first time, Function instance is created, and later it is just returned.

This allows to create function mocks as easy as in this example:

>>> factory = FunctionFactory()
<mockify.Expectation: foo()>
>>>, 2)
<mockify.Expectation: bar(1, 2)>

Then pass to some unit under test:

>>> def unit_under_test(foo, bar):
...     foo()
...     bar(1, 2)
>>> unit_under_test(,

To finally check if all mocks registered in one FunctionFactory object are satisfied using one single call:

>>> factory.assert_satisfied()

Check if all function mocks registered by this factory are satisfied.

This method simply calls mockify.engine.Registry.assert_satisfied() with names of all created mocks as arguments.

class mockify.mock.Namespace(name, registry=None, mock_class=None)

Bases: object

Used to mock functions that are behind some kind of a namespace.

New in version 0.5.

Look at following code:

if os.path.isfile(path):

It is very common pattern of how os is used by Python applications. And basically Namespace can be used to make it easier to mock such statements. You simply do it like this:

os = Namespace('os')

And now you can call such mocked statement:

assert os.path.isfile('/foo/bar/baz.txt')

There is no namespace nesting limit.

  • name

    Mock name.

    This will be used as a prefix for all namespaced mocks created by this class.

  • registry

    Instance of mockify.Registry to be used.

    If not given, a default one will be created for this mock object.

  • mock_class

    Mock class to be used for “leaf” nodes.

    By default, this is mockify.mock.Function, but you can give any other mock class here.


Check if all mocks created within this namespace are satisfied.


Name of this namespace mock.

This will be a root for all nested namespaces.