Mock Objects
Explore how to use mock objects in Python unit tests to isolate code from external services and side effects. Understand best practices and warnings about overusing mocks or monkey patching. This lesson helps you write better, more maintainable code by improving testability and recognizing code smells through unit testing.
We'll cover the following...
Another tool that will help us in our testing efforts is the use of mock objects.
There are cases where our code is not the only thing that will be present in the context of our tests. After all, the systems we design and build have to do something real, and that usually means connecting to external services (databases, storage services, external APIs, cloud services, and so on). Because they need to have those side effects, they're inevitable. As much as we abstract our code, program toward interfaces, and isolate code from external factors to minimize side effects, they will be present in our tests, and we need an effective way to handle that.
Mock objects are one of the best tactics used to protect our unit tests against undesirable side effects. Our code might need to perform ...