mockify.core - Library core¶
Classes and functions providing Mockify’s core functionality.
-
class
mockify.core.
MockInfo
(target: mockify.abc.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 agetattr()
to be used on top ofobject.__getattr__
magic method.Although this class is basically a 1-1 proxy on top of
__m_(.*)__
properties provided bymockify.abc.IMock
, some methods additionally wrap results withMockInfo
instances.Parameters: target – Mock to be inspected -
__init__
(target: mockify.abc.IMock)¶ Initialize self. See help(type(self)) for accurate signature.
-
__repr__
()¶ Return repr(self).
-
children
() → Iterator[mockify.core._inspect.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 newMockInfo
object.
-
expectations
() → Iterator[mockify.abc.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[mockify.core._inspect.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 newMockInfo
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)
-
fullname
¶ 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.New in version 0.8.
-
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.
-
name
¶ 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
-
parent
¶ A proxy to access
BaseMock.__m_parent__
.Returns
None
if target has no parent, or parent wrapped withMockInfo
object otherwise.New in version 0.8.
-
session
¶ Return
mockify.abc.ISession
object assigned to target mock.This is equivalent of using
mockify.abc.IMock.__m_session__
attribute on a target mock.
-
target
¶ Target mock that is being inspected.
New in version 0.8.
-
-
class
mockify.core.
BaseMock
(*args, **kwargs)¶ Bases:
mockify.mock._base.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.
-
class
mockify.core.
Call
(__m_fullname__: str, *args, **kwargs)¶ Bases:
mockify.abc.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, **kwargs)¶ Initialize self. See help(type(self)) for accurate signature.
-
__repr__
()¶ Return repr(self).
-
__str__
()¶ Return str(self).
-
args
¶
-
kwargs
¶
-
location
¶
-
name
¶
-
class
mockify.core.
LocationInfo
(filename: str, lineno: int)¶ Bases:
mockify.core._call._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.
New in version 0.6.
Parameters: - filename – Name of the file
- lineno – Line number in given file
-
__init__
(filename: str, lineno: int)¶ Initialize self. See help(type(self)) for accurate signature.
-
class
mockify.core.
Expectation
(expected_call: mockify.abc.ICall)¶ Bases:
mockify.abc.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 whereexpect_call(...)
method was called.-
__call__
(actual_call: mockify.abc.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: mockify.abc.ICall)¶ Initialize self. See help(type(self)) for accurate signature.
-
__repr__
()¶ Return repr(self).
-
is_satisfied
()¶
-
times
(cardinality)¶
-
will_once
(action)¶
-
will_repeatedly
(action)¶
-
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
-
actual_call_count
¶ 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()
orwill_repeatedly()
, then this counter will return total number of calls.New in version 0.6.
-
expected_call
¶ Returns expected call object assigned to this expectation.
This is used by Mockify’s internals to find expectations that match given actual call object.
-
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.
Session
¶ Bases:
mockify.abc.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)¶
-
__init__
()¶ Initialize self. See help(type(self)) for accurate signature.
-
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)¶
-
expectations
()¶
-
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: mockify.abc.IMock, *args)¶ Check if all given mocks are satisfied.
This is done by collecting all
mockify.abc.IExpectation
objects for whichmockify.abc.IExpectation.is_satisfied()
returnsFalse
.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.
satisfied
(mock: mockify.abc.IMock, *mocks)¶ Context manager wrapper for
assert_satisfied()
.Parameters: - mock – Mock object
- *args – Additional mock objects
-
mockify.core.
ordered
(mock: mockify.abc.IMock, *args)¶ 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: mockify.abc.IMock, *args)¶ 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