mockify - Library core

Library core module.

class mockify.Call(_name_, *args, **kwargs)

An object representing mock call.

Instances of this class are created when expectations are recorded or when mock is called. Call objects are comparable. Two call objects are equal if and only if:

  • mock names are the same,
  • args are the same,
  • and keyword args are the same.
Parameters:_name_ – The name of a mock

The name of a mock.


Positional args mock was called with or is expected to be called with.


Keyword args mock was called with or is expected to be called with.


Information of place in test or tested code where this call object was created.

New in version 0.6.

Return type:LocationInfo
class mockify.LocationInfo(filename, lineno)

A placeholder for file name and line number obtained from the stack.

Used by Call objects to get their location in the code. That information is later used in assertion messages.

New in version 0.6.

  • filename – Name of the file
  • lineno – Line number in given file

File name from stack.


Line number form stack.

classmethod get_external()

Factory method for creating instances of this class.

It extracts stack and finds (in reversed order) first frame that is outside of the Mockify library. Thanks to this all mock calls or expectation recordings will point to test function or tested code that uses Mockify, not to Mockify’s internals.

Return type:LocationInfo
class mockify.Session

A class providing core logic of connecting mock calls with recorded expectations.

Sessions are created for each mock automatically, or can be created explicitly and then shared across multiple mocks. While mock classes can be seen as som kind of frontends that mimic behavior of various Python constructs, session instances are some kind of backends that receive mockify.Call instances created by mocks during either mock call, or expectation recording.

Changed in version 0.6: Previously this was named Registry.


A dictionary-like object for configuring sessions.

Following options are currently available:


Can be used to override expectation class used when expectations are recorded.

By default, this is mockify.Expectation, and there is a requirement that custom class must inherit from original one.


Used to set a way of processing so called unexpected calls, i.e. calls to mocks that has no expectations recorded. Following values are supported:


This is default option.

When mock is called unexpectedly, mockify.exc.UninterestedCall exception is raised and test is terminated.

Instead of raising exception, mockify.exc.UninterestedCallWarning warning is issued, and test continues.
Unexpected calls are silently ignored.

Trigger expectation matching actual_call received from mock being called.

This method is called on every mock call and basically all actual call processing takes place here. Values returned or exceptions raised by this method are also returned or raised by mock.

Parameters:actual_call – Instance of mockify.Call class created by calling mock.

An iterator over all expectations recorded in this session.

Yields mockify.Expectation instances.


Called by mock when expectation is recorded on it.

This method creates expectation object, adds it to the list of expectations, and returns.

Return type:mockify.Expectation

Instance of mockify.Call created by mock when expect_call() was called on it.

Represents parameters the mock is expected to be called with.


Check if all registered expectations are satisfied.

This works exactly the same as mockify.assert_satisfied(), but for given session only. Can be used as a replacement for any other checks if one global session object is used.


Mark expectations matching given mock names as ordered, so they will have to be resolved in their declaration order.

This is used internally by mockify.ordered().


Called by mockify.ordered() when processing of ordered expectations is done.

Moves any remaining expectations back to the unordered storage, so they will be later displayed as unsatisfied.

class mockify.Expectation(expected_call)

An class representing single expectation.

Instances of this class are created and returned by factory expect_call() method you will use to record expectations on your mocks:

>>> from mockify.mock import Mock
>>> mock = Mock('mock')
>>> mock.expect_call(1, 2)
<mockify.Expectation: mock(1, 2)>
Parameters:expected_call – Instance of Call class containing parameters passed to expect_call() factory method that created this expectation object.

Call this expectation object.

If given call object does not match expected_call then this method will raise TypeError exception.

Otherwise, total call count is increased by one and:

  • if actions are recorded, then next action is executed and its result returned or mockify.exc.OversaturatedCall exception is raised if there are no more actions
  • if there are no actions recorded, just None is returned

Check if this expectation is satisfied.

Expectation object is satisfied if and only if:

  • total number of calls is not exceeding expected number of calls,
  • all actions (if any were recorded) are consumed.
Return type:bool

Set expected number or range of call counts.

Following values are possible:

See Setting expected call count tutorial section for more details.


Append next action to be executed when this expectation object receives a call.

Once this method is called, it returns special proxy object that you can use to mutate this expectation even further by calling one of given methods on that proxy:

Thanks to that you can record so called action chains (see Recording actions for more details).

This method can be called with any action object from mockify.actions as an argument.


Attach so called repeated action to be executed when this expectation is called.

Unlike single actions, recorded with will_once(), repeated actions are by default executed any number of times, including zero (see Repeated actions for more details).

Once this method is called, it returns a proxy object you can use to adjust repeated action even more by calling one of following methods:

  • times(), used to record repeated action call count limits (see times()).

This method accepts actions defined in mockify.actions module.


Returns expected_call parameter passed during construction.

This is used when this expectation is compared with Call object representing actual call, to find expectations matching that call.

Return type:Call

Number of matching calls this expectation object received so far.

This is relative value; if one action expires and another one is started to be executed, then the counter starts counting from 0 again. Thanks to this you’ll receive information about actual action execution count. If your expectation does not use will_once() or will_repeatedly(), then this counter will return total number of calls.

New in version 0.6.


Return object representing expected number of mock calls.

Like actual_call_count, this varies depending on internal expectation object state.

Return type:mockify.cardinality.ExpectedCallCount

Return action to be executed when this expectation receives another call or None if there are no (more) actions.

Return type:mockify.actions.Action

Check if all given mocks are satisfied.

This function collects all expectations from given mock for which Expectation.is_satisfied() evaluates to False. Finally, if at least one unsatisfied expectation is found, this method raises mockify.exc.Unsatisfied exception.

See recording-and-validating-expectations tutorial for more details.


Context manager that checks if expectations in wrapped scope are consumed in same order as they were defined.

This context manager will raise mockify.exc.UnexpectedCallOrder assertion on first found mock that is executed out of specified order.

See Recording ordered expectations for more details.


Context manager wrapper for assert_satisfied().


Context manager that replaces imported objects and functions with their mocks using mock name as a name of patched module.

It will patch only functions or objects that have expectations recorded, so all needed expectations will have to be recorded before this context manager is used.

See Patching imported modules for more details.