PyDocs

Nav

Development Tools

patch.object

unittest.mock

function of patch (source)
  • 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.

When used as a class decorator `patch.object` honours `patch.TEST_PREFIX`
for choosing which methods to wrap.
1 Example
>>> with patch.object(ProductionClass, 'method', return_value=None) as mock_method:
...     thing = ProductionClass()
...     thing.method(1, 2, 3)
...
>>> mock_method.assert_called_once_with(1, 2, 3)
from the Python docs
See Also
No see-alsos for unittest.mock/patch.object
0 Notes
No notes for patch.object