Python's unittest.mock
built-in library provides patch.object
, which let's you:
patch the named member (attribute) on an object (target) with a mock object.
With regular patch
, you can easily replace an entire object with a MagicMock
.
Is it possible to one patch an entire object (and not just one attribute) using patch.object
?
I am using Python 3.8.6.
Research
It seems this question was asked 4.5 years ago: How to mock use patch.object to patch the whole object in python
However, no one answered, and the OP also seems to have had bugs elsewhere in his system.
Motivation
Why not just use patch
? Because it's just easier, when the object is already imported, to use patch.object
on it directly, instead of having to think about what target to provide to a regular patch
.