Autospeccing and Patch

Let's learn how you can create mock objects using the concept of auto-speccing and how patch can be used to mock classes or objects.


The mock module also supports the concept of auto-speccing. The autospec allows us to create mock objects that contain the same attributes and methods of the objects that we are replacing with our mock. They will even have the same call signature as the real object! You can create an autospec with the create_autospec function or by passing in the autospec argument to the mock library’s patch decorator.

Example of autospec

For now, let’s look at an easy-to-understand example of the autospec:

Get hands-on with 1200+ tech skills courses.