• Home
  • Raw
  • Download

Lines Matching refs:Mock

6    :synopsis: Mock object library.
21 :mod:`unittest.mock` provides a core :class:`Mock` class removing the need to
30 some examples of how to use :class:`Mock`, :class:`MagicMock` and
33 Mock is designed for use with :mod:`unittest` and
60 :class:`Mock` and :class:`MagicMock` objects create all attributes and
75 >>> mock = Mock(side_effect=KeyError('foo'))
92 Mock has many other ways you can configure it and control its behaviour. For
146 Mock supports the mocking of Python :ref:`magic methods <magic-methods>`. The
156 Mock allows you to assign functions (or other Mock instances) to magic methods
157 and they will be called appropriately. The :class:`MagicMock` class is just a Mock
161 The following is an example of using magic methods with the ordinary Mock
164 >>> mock = Mock()
165 >>> mock.__str__ = Mock(return_value='wheeeeee')
199 The Mock Class
208 from unittest.mock import patch, call, Mock, MagicMock, PropertyMock, AsyncMock
211 :class:`Mock` is a flexible mock object intended to replace the use of stubs and
217 :class:`MagicMock` is a subclass of :class:`Mock` with all the magic methods
223 in a particular module with a :class:`Mock` object. By default :func:`patch` will create
224 a :class:`MagicMock` for you. You can specify an alternative class of :class:`Mock` using
228 .. class:: Mock(spec=None, side_effect=None, return_value=DEFAULT, wraps=None, name=None, spec_set=…
230 Create a new :class:`Mock` object. :class:`Mock` takes several optional arguments
231 that specify the behaviour of the Mock object:
247 * *side_effect*: A function to be called whenever the Mock is called. See
248 the :attr:`~Mock.side_effect` attribute. Useful for raising exceptions or
262 this is a new Mock (created on first access). See the
273 calling the Mock will pass the call through to the wrapped object
275 Mock object that wraps the corresponding attribute of the wrapped
294 >>> mock = Mock()
296 <Mock name='mock.method()' id='...'>
305 >>> mock = Mock()
307 <Mock name='mock.method()' id='...'>
310 <Mock name='mock.method()' id='...'>
324 >>> mock = Mock()
326 <Mock name='mock.method()' id='...'>
334 >>> mock = Mock(return_value=None)
353 >>> mock = Mock(return_value=None)
371 >>> mock = Mock(return_value=None)
385 >>> m = Mock()
400 >>> mock = Mock(return_value=None)
447 >>> mock = Mock()
460 >>> mock = Mock(some_attribute='eggs', **attrs)
476 :class:`Mock` objects limit the results of ``dir(some_mock)`` to useful results.
488 Subclasses of Mock may want to override this to customize the way
499 >>> mock = Mock(return_value=None)
510 >>> mock = Mock(return_value=None)
522 >>> mock = Mock()
530 >>> mock = Mock()
533 <Mock name='mock()()' id='...'>
538 >>> mock = Mock(return_value=3)
564 >>> mock = Mock()
573 >>> mock = Mock()
580 >>> mock = Mock(return_value=3)
592 >>> mock = Mock(side_effect=side_effect)
600 >>> m = Mock(side_effect=KeyError, return_value=3)
620 >>> mock = Mock(return_value=None)
663 >>> mock = Mock(return_value=None)
683 >>> mock = Mock()
685 <Mock name='mock.method()' id='...'>
687 <Mock name='mock.property.method.attribute()' id='...'>
741 >>> mock = Mock(spec=3)
748 >>> mock = Mock()
755 A non-callable version of :class:`Mock`. The constructor parameters have the same
756 meaning of :class:`Mock`, with the exception of *return_value* and *side_effect*
759 Mock objects that use a class or an instance as a :attr:`spec` or
762 >>> mock = Mock(spec=SomeClass)
765 >>> mock = Mock(spec_set=SomeClass())
769 The :class:`Mock` classes have support for mocking magic methods. See :ref:`magic
788 >>> mock = Mock(some_attribute='eggs', **attrs)
805 >>> mock = Mock(spec=f)
807 <Mock name='mock()' id='140161580456576'>
811 This applies to :meth:`~Mock.assert_called_with`,
812 :meth:`~Mock.assert_called_once_with`, :meth:`~Mock.assert_has_calls` and
813 :meth:`~Mock.assert_any_call`. When :ref:`auto-speccing`, it will also
886 Setting the *spec* of a :class:`Mock` or :class:`MagicMock` to an async function
898 Setting the *spec* of a :class:`Mock`, :class:`MagicMock`, or :class:`AsyncMock`
901 parent mock is :class:`AsyncMock` or :class:`MagicMock`) or :class:`Mock` (if
902 the parent mock is :class:`Mock`). All asynchronous functions will be
916 >>> mock = Mock(ExampleClass)
918 <Mock name='mock.sync_foo' id='...'>
1045 See :func:`Mock.reset_mock`. Also sets :attr:`await_count` to 0,
1066 the mock was last awaited with. Functions the same as :attr:`Mock.call_args`.
1104 Mock objects are callable. The call will return the value set as the
1105 :attr:`~Mock.return_value` attribute. The default return value is a new Mock
1107 explicitly or by calling the Mock) - but it is stored and the same one
1111 like :attr:`~Mock.call_args` and :attr:`~Mock.call_args_list`.
1113 If :attr:`~Mock.side_effect` is set then it will be called after the call has
1118 :attr:`~Mock.side_effect` an exception class or instance:
1220 Mock objects create attributes on demand. This allows them to pretend to be
1243 Mock names and the name attribute
1246 Since "name" is an argument to the :class:`Mock` constructor, if you want your
1249 :meth:`~Mock.configure_mock`::
1267 the :attr:`~Mock.method_calls` and :attr:`~Mock.mock_calls` attributes of the
1295 attach mocks that have names to a parent you use the :meth:`~Mock.attach_mock`
1313 leading and trailing double underscores). Mock doesn't create these but
1316 get a new Mock object when it expects a magic method. If you need magic
1425 :attr:`~Mock.return_value` of the mock that will be used.
1428 :attr:`~Mock.side_effect` to return a new mock each time. Alternatively you
1494 :attr:`~Mock.return_value` and :attr:`~Mock.side_effect`, of child mocks can
1958 :class:`Mock` supports mocking the Python protocol methods, also known as
1974 >>> mock = Mock()
1979 >>> mock = Mock()
1980 >>> mock.__str__ = Mock()
1985 >>> mock = Mock()
1986 >>> mock.__iter__ = Mock(return_value=iter([]))
1993 >>> mock = Mock()
1994 >>> mock.__enter__ = Mock(return_value='foo')
1995 >>> mock.__exit__ = Mock(return_value=False)
2002 Calls to magic methods do not appear in :attr:`~Mock.method_calls`, but they
2003 are recorded in :attr:`~Mock.mock_calls`.
2049 Magic Mock
2057 ``MagicMock`` is a subclass of :class:`Mock` with default implementations
2061 The constructor parameters have the same meaning as for :class:`Mock`.
2125 :attr:`~Mock.side_effect` attribute, unless you change their return value to
2176 .. [#] The function is basically hooked up to the class, but each ``Mock`` argument
2207 >>> real.method = Mock(name="method")
2222 ``sentinel.DEFAULT``). It can be used by :attr:`~Mock.side_effect`
2232 :attr:`~Mock.call_args`, :attr:`~Mock.call_args_list`,
2233 :attr:`~Mock.mock_calls` and :attr:`~Mock.method_calls`. :func:`call` can also be
2234 used with :meth:`~Mock.assert_has_calls`.
2250 multiple entries in :attr:`~Mock.mock_calls` on a mock. Manually constructing
2273 objects that are in the :attr:`Mock.call_args`, :attr:`Mock.call_args_list` and
2274 :attr:`Mock.mock_calls` attributes can be introspected to get at the individual
2277 The ``call`` objects in :attr:`Mock.call_args` and :attr:`Mock.call_args_list`
2279 in :attr:`Mock.mock_calls`, along with ones you construct yourself, are
2354 them individually out of :attr:`~Mock.call_args` and make more complex
2358 *everything*. Calls to :meth:`~Mock.assert_called_with` and
2359 :meth:`~Mock.assert_called_once_with` will then succeed no matter what was
2362 >>> mock = Mock(return_value=None)
2367 :attr:`~Mock.mock_calls`:
2398 >>> dir(Mock())
2409 >>> dir(Mock(spec=request))
2416 Many of the not-very-useful (private to :class:`Mock` rather than the thing being
2418 filtered from the result of calling :func:`dir` on a :class:`Mock`. If you dislike this
2427 >>> dir(mock.Mock())
2527 :class:`Mock` is a very powerful and flexible object, but it suffers from two flaws
2529 specific to the :class:`Mock` api and the other is a more general problem with using
2532 First the problem specific to :class:`Mock`. :class:`Mock` has two assert methods that are
2533 extremely handy: :meth:`~Mock.assert_called_with` and
2534 :meth:`~Mock.assert_called_once_with`.
2536 >>> mock = Mock(name='Thing', return_value=None)
2551 >>> mock = Mock(name='Thing', return_value=None)
2572 >>> mock = Mock(spec=request.Request)
2576 AttributeError: Mock object has no attribute 'assret_called_with'
2584 <mock.Mock object at 0x...>
2631 AttributeError: Mock object has no attribute 'assret_called_with'
2670 AttributeError: Mock object has no attribute 'a'
2694 AttributeError: Mock object has no attribute 'a'
2763 >>> mock = Mock()
2765 >>> mock.not_submock = mock.Mock(name="sample_name")