Lines Matching refs:Mock
17 from unittest.mock import Mock, MagicMock, AsyncMock, patch, call, sentinel
26 Using Mock
29 Mock Patching Methods
32 Common uses for :class:`Mock` objects include:
50 In most of these examples the :class:`Mock` and :class:`MagicMock` classes
54 Once the mock has been called its :attr:`~Mock.called` attribute is set to
55 ``True``. More importantly we can use the :meth:`~Mock.assert_called_with` or
56 :meth:`~Mock.assert_called_once_with` method to check that it was called with
75 Mock for Method Calls on an Object
95 >>> mock = Mock()
101 accessing it in the test will create it, but :meth:`~Mock.assert_called_with`
116 by modifying the mock :attr:`~Mock.return_value`. ::
147 :attr:`~Mock.mock_calls` attribute records all calls
173 >>> m = Mock()
175 <Mock name='mock.factory().deliver()' id='...'>
185 >>> mock = Mock()
192 >>> mock = Mock()
199 >>> mock = Mock(return_value=3)
205 >>> mock = Mock()
217 >>> mock = Mock()
235 A useful attribute is :attr:`~Mock.side_effect`. If you set this to an
239 >>> mock = Mock(side_effect=Exception('Boom!'))
284 :ref:`async-iterators` through ``__aiter__``. The :attr:`~Mock.return_value`
321 Creating a Mock from an Existing Object
331 :class:`Mock` allows you to provide an object as a specification for the mock,
338 >>> mock = Mock(spec=SomeClass)
350 >>> mock = Mock(spec=f)
352 <Mock name='mock()' id='140161580456576'>
435 If you want to patch with a Mock, you can use :func:`patch` with only one argument
509 understand the :attr:`~Mock.return_value` attribute. When a mock is called for
511 new :class:`Mock` is created.
516 >>> mock = Mock()
518 <Mock name='mock().foo()' id='...'>
551 We can do that in a slightly nicer way using the :meth:`~Mock.configure_mock`
555 >>> mock_response = Mock(spec=open)
556 >>> mock_backend = Mock()
566 Using :attr:`~Mock.mock_calls` we can check the chained call with a single
755 with a Mock instance instead, and isn't called with ``self``.
763 >>> mock = Mock()
782 :attr:`~Mock.call_args_list`:
784 >>> mock = Mock(return_value=None)
847 >>> from unittest.mock import Mock, patch, DEFAULT
849 ... new_mock = Mock()
880 >>> mock = Mock(side_effect=side_effect)
887 An alternative approach is to create a subclass of :class:`Mock` or
911 When you subclass ``Mock`` or ``MagicMock`` all dynamically created attributes,
971 and using :data:`~Mock.side_effect` to delegate dictionary access to a real
979 :data:`~Mock.call_args_list` to assert about how the dictionary was used:
994 An alternative to using ``MagicMock`` is to use ``Mock`` and *only* provide
997 >>> mock = Mock()
998 >>> mock.__getitem__ = Mock(side_effect=getitem)
999 >>> mock.__setitem__ = Mock(side_effect=setitem)
1039 Mock subclasses and their attributes
1042 There are various reasons why you might want to subclass :class:`Mock`. One
1058 The standard behaviour for ``Mock`` instances is that attributes and the return
1060 that ``Mock`` attributes are ``Mocks`` and ``MagicMock`` attributes are ``MagicMocks``
1080 ``Mock`` (in all its flavours) uses a method called ``_get_child_mock`` to create
1132 >>> mock = Mock()
1137 <Mock name='mock.blob()' id='...'>
1146 >>> mock = Mock()
1151 <Mock name='mock.blob.blip()' id='...'>
1156 >>> mock = Mock()
1162 <Mock name='mock.module.fooble()' id='...'>
1169 The :class:`Mock` class allows you to track the *order* of method calls on
1170 your mock objects through the :attr:`~Mock.method_calls` attribute. This
1172 however we can use :attr:`~Mock.mock_calls` to achieve the same effect.
1179 >>> manager = Mock()
1184 <Mock name='mock.foo.something()' id='...'>
1186 <Mock name='mock.bar.other.thing()' id='...'>
1199 them to a manager mock using the :meth:`~Mock.attach_mock` method. After
1219 :meth:`~Mock.assert_has_calls` method. This takes a list of calls (constructed
1221 :attr:`~Mock.mock_calls` then the assert succeeds.
1253 defined classes). To use :meth:`~Mock.assert_called_with` we would need to pass
1265 >>> mock = Mock(return_value=None)