Pattern – spies

Mocks allow us to replace an object or class with a dummy mock object. We've seen how we can then make the mock return predefined values, so that the class under test doesn't even know that it has made a call to a mock object. However, sometimes we might want to just record that the call was made to an object, but allow the execution flow to continue to the real object and return. Such an object is known as a spy. A spy retains the functionality of recording calls and being able to assert on the calls afterwards, but it does not replace a real object like a regular mock does.

The wraps parameter when creating a mock.Mock object allows us to create spy behavior in our code. It takes an object as a value, and all calls to the mock ...

Get Test-Driven Python Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.