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 ...

Get Flask Framework Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.