Mock for tests
Key 1: Mock what you do not have.
When we are using test driven development, we have to write test cases for the components that rely on other components that are not written yet or take a lot of time to execute. This is close to impossible until we create mocks and stubs. In this scenario, stubs or mocks are very useful. We use a fake object instead of a real one to write the test case. This can be made very easy if we use tools that are provided by the language. For example, in the following code, we only have the interface for the worker class, and no real implementation of it. We want to test the assign_if_free
function.
Instead of writing any stub ourselves, we use the create_autospec
function to create a mock object from the definition of the Worker abstract class. We also set up a return value for the function call of checking whether worker was busy or not:
import six import unittest import sys import abc if sys.version_info[0:2] >= (3, 3): from unittest.mock...