mockify.mock - Classes for creating and inspecting mocks

Module containing types used to mock things.

These can be considered as frontends on top of Mockify’s core functionality.

class mockify.mock.Mock(name, **kwargs)

General purpose mock class.

This class is used to:

  • create mocks of functions,
  • create mocks of objects with methods, setters and getters,
  • create mocks of modules,
  • create ad-hoc data objects.

No matter what you will be mocking, for all cases creating mock objects is always the same - by giving it a name and optionally session. Mock objects automatically create attributes on demand, and that attributes form some kind of nested or child mocks.

To record expectations, you have to call expect_call() method on one of that attributes, or on mock object itself (for function mocks). Then you pass mock object to unit under test. Finally, you will need mockify.core.assert_satisfied() function or mockify.core.satisfied() context manager to check if the mock is satisfied.

Here’s an example:

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

def caller(func, a, b):
    func(a + b)

def test_caller():
    func = Mock('func')
    func.expect_call(5)
    with satisfied(func):
        caller(func, 2, 3)

See Creating mocks and recording expectations for more details.

Changed in version 0.8: Now this class inherits from mockify.mock.BaseMock

New in version 0.6.

__init__(name, **kwargs)

Initialize self. See help(type(self)) for accurate signature.

__m_children__()

Iterator over BaseMock objects representing direct children of this mock.

This should not include grandchildren.

__m_expectations__()

Iterator over mockify.core.Expectation objects recorded for this mock.

It should not include expectations recorded on mock’s children. To get all expectations (including children), use __m_walk__().

__setattr__(name, value)

Implement setattr(self, name, value).

__getattribute__(name)

Return getattr(self, name).

__call__(*args, **kwargs)

Call self as a function.

class mockify.mock.FunctionMock(name, **kwargs)

Class for mocking Python functions.

This is most basic mock class Mockify provides. It can be used as a standalone mock, for mocking standalone functions in tests, or to build more complex mocks.

Here’s example usage:

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

func = FunctionMock('func')
func.expect_call(1, 2, 3).will_once(Return(123))
with satisfied(func):
    assert func(1, 2, 3) == 123
Parameters:name

Name of mocked function.

This must a valid Python identifier or series of valid Python identifiers concatenated with a period sign (f.e. foo.bar.baz).

Changed in version 0.9: Removed parameter session in favour of **kwargs; session handling is now done by BaseMock class.

New in version 0.8.

__init__(name, **kwargs)

Initialize self. See help(type(self)) for accurate signature.

__m_children__()

Iterator over BaseMock objects representing direct children of this mock.

This should not include grandchildren.

__m_expectations__()

Iterator over mockify.core.Expectation objects recorded for this mock.

It should not include expectations recorded on mock’s children. To get all expectations (including children), use __m_walk__().

__call__(*args, **kwargs)

Call self as a function.

class mockify.mock.MockFactory(name=None, mock_class=None, **kwargs)

A factory class used to create groups of related mocks.

This class allows to create mocks using class given by mock_class ensuring that:

  • names of created mocks are unique,
  • all mocks share one common session object.

Instances of this class keep track of created mocks. Moreover, functions that would accept Mock instances will also accept MockFactory instances, so you can later f.e. check if all created mocks are satisfied using just a factory object. That makes it easy to manage multiple mocks in large test suites.

See Managing multiple mocks for more details.

Changed in version 0.8: Now it inherits from mockify.mock.BaseMock, as this class is more or less special kind of mock.

New in version 0.6.

Parameters:
  • name

    This is optional.

    Name of this factory to be used as a common prefix for all created mocks and nested factories.

  • mock_class

    The class that will be used by this factory to create mocks.

    By default it will use Mock class.

Changed in version 0.9: Removed parameter session in favour of **kwargs; session handling is now done by BaseMock class.

__init__(name=None, mock_class=None, **kwargs)

Initialize self. See help(type(self)) for accurate signature.

__m_children__()

Iterator over BaseMock objects representing direct children of this mock.

This should not include grandchildren.

__m_expectations__()

Iterator over mockify.core.Expectation objects recorded for this mock.

It should not include expectations recorded on mock’s children. To get all expectations (including children), use __m_walk__().

__repr__()

Return repr(self).

mock(name)

Create and return mock of given name.

This method will raise TypeError if name is already used by either mock or child factory.

factory(name)

Create and return child factory.

Child factory will use session from its parent, and will prefix all mocks and grandchild factories with given name.

This method will raise TypeError if name is already used by either mock or child factory.

Return type:MockFactory
mockify.mock.ABCMock(name, abstract_base_class, **kwargs)

Factory function for creating mocks that implement given abc.ABC subclass.

This class is meant to be used with interfaces containing abstract methods. It performs a lookup on the interface and allows to record expectations only on methods that are defined in the interface. Moreover, it also checks argument names and disallow recording calls with invalid arguments; everything must match the definition of the interface.

Here’s an example:

import abc

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

class Interface(abc.ABC):

    @abc.abstractmethod
    def method(self, a, b):
        pass

mock = ABCMock('mock', Interface)
mock.method.expect_call(1, 2).will_once(Return(123))
with satisfied(mock):
    assert mock.method(1, 2) == 123
Parameters:
  • name – Mock name
  • abstract_base_class – Subclass of abc.ABC to be used as source of abstract methods that will be implemented by this mock.

Changed in version 0.9: * Now this is a factory function returning mock object * Parameter session is removed in favour of **kwargs; session is now handled by BaseMock class

New in version 0.8.