Contribute Media
A thank you to everyone who makes this possible: Read More

Unit Testing with Requests

Description

If you're working with APIs or the web in python you will (or should) have heard of requests. The 'HTTP for Humans' library is one of python's most popular and most downloaded libraries and is referenced by the urllib docs as the recommended way to do HTTP requests in python.

As strict unit testers of our code we need to assert that our projects are making the expected HTTP requests however don't want to be making actual network connections. Most people start with simply mocking this requests interface but this is much more complex than it appears on first glance and frequently ends up replicating large chunks of the interface or skipping mocked out functions within your code.

In this talk I'll cover two libraries that can help you mock out your HTTP requests: requests-mock and betamax.

requests-mock is the extended and more correct version of mocking out the interface. It lets you ensure that the requests you expect are going over the wire, but ensures that all the bytes/strings, json decoding and other helper features that requests offers work, without your mocks becoming unmanageable.

Betamax gives you functional style testing without the network connection. It allows you to record real interactions through requests, store that to file, and replay them later in testing.

Between them we can assert our behaviour both against our local, constructed responses, and real recorded responses our application has made.

With a small example app I'll demonstrate how you can use these libraries, the different benefits of each, and how to structure your tests to make the most use of them.

Details

Improve this page