Using mocking to avoid real API access
We are aware of how testing works, but now, let's say we have a third-party application/service integrated via API calls with our application. It will 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 e-mails. In this recipe, we will integrate our application with the geoip
library and then test it via mocking.
Getting ready
First, we need to install the mock
and geoip
libraries and the corresponding database:
$ pip install mock $ pip install python-geoip $ pip install python-geoip-geolite2
Now, let's say we want to store the location of the user who creates a product (think of a scenario where the application is administered via multiple locations around the globe).
We need...