unittest.mock.sentinel

unittest.mock.sentinel The sentinel object provides a convenient way of providing unique objects for your tests. Attributes are created on demand when you access them by name. Accessing the same attribute will always return the same object. The objects returned have a sensible repr so that test failure messages are readable.

unittest.mock.PropertyMock

class unittest.mock.PropertyMock(*args, **kwargs) A mock intended to be used as a property, or other descriptor, on a class. PropertyMock provides __get__() and __set__() methods so you can specify a return value when it is fetched. Fetching a PropertyMock instance from an object calls the mock, with no args. Setting it calls the mock with the value being set. >>> class Foo: ... @property ... def foo(self): ... return 'something' ... @foo.setter ... def foo(s

unittest.mock.patch.stopall()

patch.stopall() Stop all active patches. Only stops patches started with start.

unittest.mock.patch.object()

patch.object(target, attribute, new=DEFAULT, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs) patch the named member (attribute) on an object (target) with a mock object. patch.object() can be used as a decorator, class decorator or a context manager. Arguments new, spec, create, spec_set, autospec and new_callable have the same meaning as for patch(). Like patch(), patch.object() takes arbitrary keyword arguments for configuring the mock object it creates.

unittest.mock.patch.multiple()

patch.multiple(target, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs) Perform multiple patches in a single call. It takes the object to be patched (either as an object or a string to fetch the object by importing) and keyword arguments for the patches: with patch.multiple(settings, FIRST_PATCH='one', SECOND_PATCH='two'): ... Use DEFAULT as the value if you want patch.multiple() to create mocks for you. In this case the created mocks are passed into a

unittest.mock.patch.dict()

patch.dict(in_dict, values=(), clear=False, **kwargs) Patch a dictionary, or dictionary like object, and restore the dictionary to its original state after the test. in_dict can be a dictionary or a mapping like container. If it is a mapping then it must at least support getting, setting and deleting items plus iterating over keys. in_dict can also be a string specifying the name of the dictionary, which will then be fetched by importing it. values can be a dictionary of values to set in the

unittest.mock.patch()

unittest.mock.patch(target, new=DEFAULT, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs) patch() acts as a function decorator, class decorator or a context manager. Inside the body of the function or with statement, the target is patched with a new object. When the function/with statement exits the patch is undone. If new is omitted, then the target is replaced with a MagicMock. If patch() is used as a decorator and new is omitted, the created mock is pass

unittest.mock.NonCallableMock

class unittest.mock.NonCallableMock(spec=None, wraps=None, name=None, spec_set=None, **kwargs) A non-callable version of Mock. The constructor parameters have the same meaning of Mock, with the exception of return_value and side_effect which have no meaning on a non-callable mock.

unittest.mock.NonCallableMagicMock

class unittest.mock.NonCallableMagicMock(*args, **kw) A non-callable version of MagicMock. The constructor parameters have the same meaning as for MagicMock, with the exception of return_value and side_effect which have no meaning on a non-callable mock.

unittest.mock.mock_open()

unittest.mock.mock_open(mock=None, read_data=None) A helper function to create a mock to replace the use of open(). It works for open() called directly or used as a context manager. The mock argument is the mock object to configure. If None (the default) then a MagicMock will be created for you, with the API limited to methods or attributes available on standard file handles. read_data is a string for the read(), readline(), and readlines() methods of the file handle to return. Calls to thos