mockify.mock - Classes for creating and inspecting mocks¶
An object used to inspect given target mock.
This class provides a sort of public interface on top of underlying
Mockinstance, that due to its specific features has no methods or properties publicly available.
Parameters: mock – Instance of
Mockobject to be inspected
Reference to target mock object.
Name of target mock.
An iterator over target mock’s direct children.
MockInfoobject for each target mock’s children.
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
Mockinstances will also accept
MockFactoryinstances, 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.
- name –
This is optional.
Name of this factory to be used as a common prefix for all created mocks and nested factories.
- session –
mockify.Sessionto 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
Create and return mock of given name.
This method will raise
TypeErrorif name is already used by either mock or child factory.
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.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.