mockify.mock - Classes for creating and inspecting mocks

class mockify.mock.MockInfo(mock)

An object used to inspect given target mock.

This class provides a sort of public interface on top of underlying Mock instance, that due to its specific features has no methods or properties publicly available.

Parameters:mock – Instance of Mock object to be inspected
mock

Reference to target mock object.

name

Name of target mock.

session

Instance of mockify.Session assigned to given target mock.

expectations()

An iterator over all mockify.Expectation objects recorded for target mock.

children()

An iterator over target mock’s direct children.

It yields MockInfo object for each target mock’s children.

walk()

Recursively iterates in depth-first order over all target mock’s children and yields MockInfo object for each found child.

It always yields self as first element.

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.

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.

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.Mock(name, session=None)

All-in-one mocking utility.

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.

New in version 0.6.