Do not kill the mockingbird – how to level up your unit testing with mocking
The goal of unit testing is to make sure that a particular unit of code works on its own, but if you are using a library, it could be hard to force that library to return a specific value or values to ensure that the code developed using it works on its own. Let’s see that with an example. Imagine your code does not have access to the internet, and, therefore, the following piece will break if tested directly:
request_call.py
import requests def invoke_get() -> bool: response = requests.get("https://www.packtpub.com") if response.status_code == 200: return True return False
Tip
Don’t panic about this code; we will explain requests and responses in the next chapter.
To test this unit, we...