Using unittest.mock
In this section, we'll take a look at a subpackage of unit tests, called mock. The tools in the mock
package help us keep our test isolated, so they aren't made to succeed or fail based on the behavior of the code, which isn't supposed to be covered by the test.
We talked about how important it is that unit tests only interact with a small section of code, but how can we arrange for this when so many pieces of code interact with objects and functions originating from all over the source tree? One answer is that we can replace those objects and functions with mock objects.
What is a mock object?
A mock object is a clever piece of code; it could pretend to be almost any kind of object or function, but instead of doing whatever the original did, it just records what is done with it so we can check it later. Let's play with a mock object for a moment to get a feel for them:

Refer to the preceding screenshot. We can access pretty much any attribute of the mock object without defining...