Change the future

Thursday 1:20 p.m.–4:40 p.m.

Intermediate Twisted: Test-Driven Networking Software

Itamar Turner-Trauring

Audience level:
Distributed Computing


Writing tests for networking code presents special challenges, which Twisted tries to address. In this tutorial you will implement a toy HTTP server using test-first methodology: first the tests are written, and then the code. You will learn how to write well-tested network applications using Twisted, and the general virtues of doing test-driven development.


Unit testing is one of the most important methods for building more reliable, robust software; test-driven development, where tests are written first, even more so. But testing network applications presents additional difficulties:

  • Data can arrive over the network with arbitrary delays, in arbitrary chunks,
  • Connections can break at random,
  • Timeouts are an important feature, but a unittest that takes 2 hours to pass is unacceptable,
  • Relying on actual networking for tests leads to more fragile tests,
  • etc.

In order to deal with these issues, the Twisted event-driven networking framework provides an extensive set of functionality for testing, making it an excellent choice for building reliable applications.

This tutorial aims to teach you how to write well-tested network applications with Twisted using a series of hands-on exercises. We will begin with a quick lecture on how to test Twisted code. Then you will get your hands dirty coding by trying to make a provided set of tests pass; the result will be a toy HTTP server. Next, you will be given a set of test descriptions, and will need to write both tests and code. For the final exercise, you will need to come up with the list of tests yourself, as you would in real world development.

Prerequisites: Intermediate knowledge of Python: classes, functions, etc.. A basic understanding of Twisted (protocols, transports, Deferreds), e.g. as provided by the introductory Twisted tutorial. Previous experience with other event-driven frameworks may suffice, however, given enough programming experience. Some experience with the unittest module would also be very helpful.

Please arrive with a laptop configured with a recent version of Twisted (12.2 or later) and Python 2.7. Ubuntu, Debian or some other version of Linux or Unix (e.g. OS X) is highly recommended, but not necessary. If you’re using Windows please make very very sure you have a Twisted development environment set up before the class has started; in particular you should be able to run the trial command-line program.

Update: See updated tutorial preparation instructions at Intermediate Twisted: Test-Driven Networking Software