mockify.mock - Classes for creating and inspecting mocks

class mockify.mock.MockInfo(target)

A class for inspecting mocks.

This class simplifies access to mock’s special properties and methods defined in BaseMock, but wraps results (when applicable) with MockInfo instances. If you need to access mock metadata in your tests, then this class is a recommended way to do this.

Parameters:target – Instance of BaseMock object to be inspected
__init__(target)

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

__repr__()

Return repr(self).

mock

Target mock that is being inspected.

Deprecated since version 0.8: This is deprecated since version 0.8 and will be dropped in one of upcoming releases. Use target instead.

target

Reference to BaseMock being inspected.

New in version 0.8.

parent

A proxy to access BaseMock.__m_parent__.

Returns None if target has no parent, or parent wrapped with MockInfo object otherwise.

New in version 0.8.

name

A proxy to access BaseMock.__m_name__ of target mock.

Changed in version 0.8: It is no longer full name; for that purpose use new fullname

fullname

A proxy to access BaseMock.__m_fullname__ of target mock.

New in version 0.8.

session

A proxy to access BaseMock.__m_session__ of target mock.

expectations()

An iterator over results returned by BaseMock.__m_expectations__() method.

children()

An iterator over results returned by BaseMock.__m_children__() method.

It wraps each found child with a MockInfo object.

__weakref__

list of weak references to the object (if defined)

walk()

An iterator over results returnend by BaseMock.__m_walk__() method.

It wraps each found child with a MockInfo object.

class mockify.mock.BaseMock

Abstract base class for all mock classes.

In Mockify, mocks are composed in a tree-like structure. For example, to mock object with a methods we compose object mock (a root) and then supply it with leafs (or children), each representing single mocked method of that object. This class provides methods and properties for Mockify engine to walk through such defined structure.

If you need to declare your own mocks, make sure you implement this interface.

New in version 0.8.

__repr__()

Return repr(self).

__m_fullname__

Full name of this mock.

It is composed of parent’s __m_fullname__ and current __m_name__. If mock has no parent, then this will be same as __m_name__.

__m_parent__

Weak reference to BaseMock that is a parent of this mock.

If mock has no parent, this should be set to None.

Note

Make sure this property is always set in subclass to either parent object or None - otherwise you may get errors.

__m_walk__()

Recursively iterate over BaseMock object yielded by __m_children__() method.

It always yields self as first element.

This method is used by Mockify internals to collect all expectations recorded for mock and all its children.

__m_name__

Name of this mock.

__m_session__

Instance of mockify.Session used by this mock.

This should always be the same object for mock and all its children.

__m_expectations__()

Iterator over mockify.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__().

__m_children__()

Iterator over BaseMock objects representing direct children of this mock.

This should not include grandchildren.

__weakref__

list of weak references to the object (if defined)

class mockify.mock.Mock(name, session=None)

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.assert_satisfied() function or mockify.satisfied() context manager to check if the mock is satisfied.

Here’s an example:

from mockify 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, session=None)

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

class mockify.mock.FunctionMock(name, session=None)

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 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).

  • session

    Instance of mockify.Session to be used by this mock.

    Default one will be created if not given, although some of Mockify features require sharing a session between several mocks.

New in version 0.8.

__init__(name, session=None)

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

__m_name__

Name of this mock.

__m_session__

Instance of mockify.Session used by this mock.

This should always be the same object for mock and all its children.

__m_children__()

Iterator over BaseMock objects representing direct children of this mock.

This should not include grandchildren.

__m_expectations__()

Iterator over mockify.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, session=None, mock_class=None)

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.

  • session

    Instance of mockify.Session to be used.

    If not given, a default session will be created and shared across all mocks created by this factory.

  • mock_class

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

    By default it will use Mock class.

__init__(name=None, session=None, mock_class=None)

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

__m_name__

Name of this mock.

__m_fullname__

Full name of this mock.

It is composed of parent’s __m_fullname__ and current __m_name__. If mock has no parent, then this will be same as __m_name__.

__m_session__

Instance of mockify.Session used by this mock.

This should always be the same object for mock and all its children.

__m_parent__

Weak reference to BaseMock that is a parent of this mock.

If mock has no parent, this should be set to None.

Note

Make sure this property is always set in subclass to either parent object or None - otherwise you may get errors.

__m_children__()

Iterator over BaseMock objects representing direct children of this mock.

This should not include grandchildren.

__m_expectations__()

Iterator over mockify.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
class mockify.mock.ABCMock

Mock class 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 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.
  • session

    Instance of mockify.Session to be used for this mock.

    Default one will automatically be used if left empty.

New in version 0.8.

static __new__(cls, name, abstract_base_class, session=None)

Create and return a new object. See help(type) for accurate signature.

__weakref__

list of weak references to the object (if defined)