mockify.core - Library core

Classes and functions providing Mockify’s core functionality.

class mockify.core.BaseMock(*args, **kwargs)

Bases: BaseMock

Deprecated since version 0.13: This class was moved and is currently available as mockify.mock.BaseMock class. Old import will stop working in one of upcoming releases.

__init__(*args, **kwargs)
class mockify.core.Call(__m_fullname__: str, *args: Any, **kwargs: Any)

Bases: ICall

Default implementation of the mockify.abc.ICall interface.

Parameters:
  • __m_fullname__ – The full name of a mock

  • *args – Positional arguments

  • **kwargs – Named arguments

Changed in version 0.13: Now this inherits from mockify.abc.ICall abstract base class

__init__(__m_fullname__: str, *args: Any, **kwargs: Any)
__repr__()

Return repr(self).

__str__()

Return str(self).

property args

See mockify.abc.ICall.args.

property kwargs

See mockify.abc.ICall.kwargs.

property location

See mockify.abc.ICall.location.

property name

See mockify.abc.ICall.name.

class mockify.core.Expectation(expected_call: ICall)

Bases: IExpectation

Default implementation of the mockify.abc.IExpectation interface.

Instances of this class are created and returned when expectations are recorded on mocks.

Here’s an example:

>>> from mockify.mock import Mock
>>> mock = Mock('mock')
>>> mock.expect_call(1, 2)
<mockify.core.Expectation: mock(1, 2)>
Parameters:

expected_call

Instance of mockify.abc.ICall object that was created during expectation recording.

Note

Value of mockify.abc.ICall.location property of given call object will point to the place in user’s test code where expect_call(...) method was called.

__call__(actual_call: ICall)

Consume this expectation object.

This method requires given actual_call object to satisfy following condition:

self.expected_call == actual_call

In other words, given actual_call must be equal to current expected_call object, as consuming expectation with a non-matching actuall call does not make sense and is considered as a bug.

Parameters:

actual_call

Instance of mockify.abc.ICall object that was created when corresponding mock was called.

Note

Value of mockify.abc.ICall.location property of given call object will point to the place in user’s tested code where the mocked method or function was called.

__init__(expected_call: ICall)
__repr__()

Return repr(self).

is_satisfied()

See mockify.abc.IExpectation.is_satisfied().

times(cardinality)

See mockify.abc.IExpectation.times().

will_once(action)

See mockify.abc.IExpectation.will_once().

will_repeatedly(action)

See mockify.abc.IExpectation.will_repeatedly().

property action

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

Return type:

mockify.actions.Action

property actual_call_count: int

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.

Added in version 0.6.

property expected_call: ICall

Returns expected call object assigned to this expectation.

This is used by Mockify’s internals to find expectations that match given actual call object.

property expected_call_count

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

class mockify.core.LocationInfo(filename: str, lineno: int)

Bases: _CallLocation

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

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

Deprecated since version 0.13: This is now made private and will be completely removed in next major release.

Added in version 0.6.

Parameters:
  • filename – Name of the file

  • lineno – Line number in given file

__init__(filename: str, lineno: int)
class mockify.core.MockInfo(target: IMock)

Bases: object

A class for inspecting mocks.

This class simplifies the use of Mockify-defined special methods and properties that must be implemented by mockify.abc.IMock subclasses. It is like a getattr() to be used on top of object.__getattr__ magic method.

Although this class is basically a 1-1 proxy on top of __m_(.*)__ properties provided by mockify.abc.IMock, some methods additionally wrap results with MockInfo instances.

Parameters:

target – Mock to be inspected

__init__(target: IMock)
__repr__()

Return repr(self).

children() Iterator[MockInfo]

Return iterator over target mock’s direct children.

This uses mockify.abc.IMock.__m_children__() method on a target mock behind the scenes, but wraps each returned child with a new MockInfo object.

expectations() Iterator[IExpectation]

Return iterator over expectation objects created on target mock.

This is equivalent of using mockify.abc.IMock.__m_expectations__() method on a target mock.

walk() Iterator[MockInfo]

Recursively iterate over tree structure of given target mock.

Behind the scenes this uses mockify.abc.IMock.__m_walk__() method on a target mock, but wraps each yielded child with a new MockInfo object.

It always yields self as a first generated item.

Changed in version 0.13: Now this is simply calling mockify.abc.IMock.__m_walk__() behind the scenes.

__weakref__

list of weak references to the object (if defined)

property fullname: str

Return full name of target mock.

This is equivalent of using mockify.abc.IMock.__m_fullname__ attribute on a target mock.

Changed in version 0.13: Now this is not calculating full name, but simply is calling mockify.abc.IMock.__m_fullname__ on target mock.

Added in version 0.8.

property mock: IMock

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.

property name: str

Return name of target mock.

This is equivalent of using mockify.abc.IMock.__m_name__ attribute on a target mock.

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

property parent: MockInfo

A proxy to access BaseMock.__m_parent__.

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

Added in version 0.8.

property session: ISession

Return mockify.abc.ISession object assigned to target mock.

This is equivalent of using mockify.abc.IMock.__m_session__ attribute on a target mock.

property target: IMock

Target mock that is being inspected.

Added in version 0.8.

class mockify.core.Session

Bases: ISession

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.core.Call instances created by mocks during either mock call, or expectation recording.

Changed in version 0.6: Previously this was named Registry.

__call__(actual_call)

See mockify.abc.ISession.__call__().

__init__()
assert_satisfied()

Check if all registered expectations are satisfied.

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

disable_ordered()

Called by mockify.core.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.

enable_ordered(names)

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.core.ordered().

expect_call(expected_call)

See mockify.abc.ISession.expect_call().

expectations()

See mockify.abc.ISession.expectations().

property config

A dictionary-like object for configuring sessions.

Following options are currently available:

'expectation_class'

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

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

'uninterested_call_strategy'

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:

'fail'

This is default option.

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

'warn'

Instead of raising exception, mockify.exc.UninterestedCallWarning warning is issued, and test continues.

'ignore'

Unexpected calls are silently ignored.

mockify.core.assert_satisfied(mock: IMock, *args: IMock)

Check if all given mocks are satisfied.

This is done by collecting all mockify.abc.IExpectation objects for which mockify.abc.IExpectation.is_satisfied() returns False.

If there are unsatisfied expectations present, then mockify.exc.Unsatisfied exception is raised and list of found unsatisfied expectations is reported.

Parameters:
  • mock – Mock object

  • *args – Additional mock objects

mockify.core.ordered(mock: IMock, *args: IMock)

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.

Parameters:
  • mock – Mock object

  • *args – Additional mock objects

mockify.core.patched(mock: IMock, *args: IMock)

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.

Parameters:
  • mock – Mock object

  • *args – Additional mock objects

mockify.core.satisfied(mock: IMock, *mocks: IMock)

Context manager wrapper for assert_satisfied().

Parameters:
  • mock – Mock object

  • *args – Additional mock objects