Using mocking to avoid external API access
We are aware of how testing works, but now, let’s imagine we have a third-party application/service integrated via API calls with our application. It would not be a great idea to make calls to this application/service every time tests are run. Sometimes, these can be paid, too, and making calls during tests can not only be expensive but also affect the statistics of that service. Mocking plays a very important role in such scenarios. The simplest example of this can be mocking SMTP for emails. In this recipe, we will integrate our application with the geoip2 library and then test it via mocking.
Getting ready
In Python 3, mock has been included as a standard package in the unittest library.
For the purpose of this recipe, we first need to install the geoip2 library and the corresponding database:
$ pip install geoip2
You also need to download the free geoip database from the MaxMind (https://dev.maxmind.com/geoip/geolite2...