Sunday, September 21, 2014

Book Review: Test-Driven Development with Python by Harry Percival; O’Reilly Media

This is an incredibly thorough book from Harry Percival.
From start to finish the reader is stepped through the creation of a Python3/Django-based application with great rigour following the clearly described principles and methods of Test-Driven Development as espoused by the (Obey the) Testing Goat.  With this book you will learn TDD but also many tools for Python/Django/Web-development and you’ll be able to reproduce all the code yourself, or obtain the code chapter by chapter from github.

What’s incredible is the number of tools and frameworks which are covered in this book, with such attention to detail on all the installation/configuration/usage steps so that you can follow along.

The very first test you’ll write is a functional test to check that you have Django up and running.  From there on, not a line of functional code is written without it’s test case having been written first, and that’s exactly the way it should be according to TDD philosophy.
The book has been updated to use Django 1.7 - which was in pre-release when the book was written, but has now been released in Sept. 2014.

I recommend a visit to the author’s web site for the book,, to learn more about the book and even read the book online as a prelude to actually buying the book.

The book covers an extensive list of pre-requisites so that all readers are on the same page before providing an introduction to the concepts of TDD.  The Pre-requisites section includes the use of git, Python3, Django1.7, and reflections on use of Windows and IDEs.

Every code snippet in the book includes the relative path to the file at top right.
This is extremely useful, and makes all the difference, allowing to follow along with the book step by step as we jump from file to file in a well structured application and its’ test code.

I really appreciate that the example souce code is available for the book on github, with a separate branch for each chapter.  I stepped through the book page by page but inevitably made some typing errors, so being able to compare with that chapters’ official code helped me to stay on track.

It was surprising at each step to read “now let’s do xxxx” and then the following code is changes to the tests before doing the declared code change - that’s exactly the way it should be in test-driven development.

It’s impressive how the author advances in a step-wise fashion, often introducing code which is in fact temporary because we’ll later be shown a better way of doing things (e.g. defining the expected html response directly in the test, then modifying the test to refer to the template file contents instead).   This
is extremely powerful as the initial code often introduces useful concepts, the reader is led from the initial solution to a better one thus reinforcing the point - this is very effective teaching!

I should point out that if I am experienced with Linux, Windows, Python, Git I have near to zero experience with Django and several other concepts here.  So I was impressed that despite my knowledge gaps I could follow along hitting exactly the same errors as the author and fixing them exactly as described.

I decided to use my Windows8/Cygwin environment to run through the book, updating my existing Anaconda installation (rather than my Cygwin installation), so I documented how I did this in this blog post.
It’s impressive the attention to detail in this book covering all the practical details needed to reproduce the examples, including anticipating problems with Windows, e.g. with a pointer to pre-compiled Windows binaries for Fabric.

The book is divided into 3 sections.

Part I. The Basics of TDD & Django
This section of the book gets us up and running with the basics of testing a Django application using functional and unit testing following the TDD methodology.
At the end of this section we have a basic application for managing to-do lists using a simple sqlite3 database and a set of unit and functional tests used to check that we can create and consult independent lists.
Use of tests to validate code refactoring.
This alone could have been a book, stepping us through the creation of a Django-based  application for managing to-do lists using a simple sqlite3 database and a set of unit and functional tests used to check that we can create and consult independent lists.  The TDD methodology is clearly described as we write tests before code and use the tests to double check when refactoring code.

Part II. Web Development SIne Qua Nons
The second section treats many topics which would inevitably be encountered in developing, staging and deploying a real web site.  Use of Bootstrap and CSS is covered, then we look at how to produce a staging site, fully tested of course, automating the setup using Fabric, before covering Django’s form classes, duplicate item handling and the use of qunit for Javascript/jQuery testing.

Part III. More Advanced Topics
The third section “Advanced Topics” covers user authentication using Mozillas’ Persona, Mocking in Javascript, Ajax testing, and then Mocking in Python.  Even Continuous Integration using Jenkins is covered.

I thoroughly recommend this book which ably describes the TDD method, but will also step you through the use of many of the useful tools and frameworks a professional developer would use.

No comments:

[Conference -] Developing Micro-services on Kubernetes

In April I had the chance to present at , first in Warsaw on Apr 24th, and then in Wroclaw ("wroslof" was my best at...