Tuesday, January 29, 2013

Test-Driven Development with Twisted: A PyCon 2013 Tutorial

Testing network applications is hard: the order of events is unpredictable, the passage of time is important, and the sources of errors are many. At PyCon this year I will be teaching a three-hour tutorial on test-driven development with Twisted, demonstrating how to build well-tested network applications.

(As a reminder, I'm also teaching a two day Twisted class in San Francisco with Jean-Paul Calderone, on the Monday and Tuesday before PyCon; early bird pricing expires Feb 15th. The material in the testing tutorial is also included in the longer class.)

In the lecture part of the tutorial I will cover:
  • Testing network protocols in a deterministic manner (no need for actual TCP connections).
  • Testing the passage of time (no need to wait 2 hours in your test to prove that a timeout is hit).
  • Twisted's testing infrastructure for running the reactor and handling Deferreds.
Once that is done, students will begin a hands-on lab, implementing a HTTP server from scratch. I will provide pre-written unit tests, and students will write code to make these tests pass, with help from me (and perhaps an assistant or two depending on number of students).

The lessons here are implicit in the design of the tests, and the design of the server as shaped by the tests. If anything these lessons are more important than understanding Twisted's testing APIs:
  • The scoping of tests into small units of work.
  • Separation of concerns - parsing/generating bytes vs. business logic.
  • Design patterns for Deferred APIs.
  • Building robust network applications, including dealing with bad input and timeouts.
  • Separation of library code and application configuration.
Students who finish early can move on to a more difficult exercise, implementing both the tests and logic for an HTTP client, but benefiting from the ability to ask for in-person help.

You can sign up at the PyCon website as part of registration, or read more on the tutorial's PyCon web page.

No comments:

Post a Comment