Mock it Til’ You Make It: Test Django with mock and httpretty
In this tutorial, we’ll learn how to use Python’s mock and httpretty libraries to test the parts of a Django app that communicate with external services. These could be 3rd party APIs, web sites you scrape, or essentially any resource which you don’t control and is behind a network boundary. In addition to that, we’ll also take a brief look into Django’s RequestFactory class and learn how we can use it to test our Django views. 🕵
In order to illustrate how all these pieces fit together, we’ll write a simple Django app — “Hacker News Hotline”. It will fetch today’s headlines from Hacker News and serve them behind a TwiML endpoint, so anyone calling our Twilio number can hear the latest headlines on Hacker News.
Right, sounds like a plan. Let’s do this!
Python Environment Setup
Before we get our hands dirty, there ...