Session: Learn Test-Driven-Development and Django by building a simple web application from scratch.

Tags:
, , ,
Location:
A03/A04
Duration:
July 23, 2014 14:00 - 17:00
Language:
English
Target-audience:
Novice

We'll cover unit testing, Django models, views and templates, as well as using Selenium to open up a real web browser for functional tests. This talk is intended for audiences new to TDD, new to Django, and even new to Python!

The aim is to cover the basics of setting up a simple Django site, but using full, rigorous TDD at every step along the way. The material covered in this training is all available online as part of Harry Percival's great book "Obey the testing goat"

We'll learn how to set up functional tests with Selenium, how to set up Django, how to run Django unit tests, how TDD actually works in practice, the unit test / code cycle where we re-run the tests after each tiny, incremental change to the code, as well as all the basics of Django. We'll talk about what to test, what not to test, what the point of all this testing is anyway, and I promise to make it all at least moderately entertaining.

Plus it's all in Python 3!

Part 0 -- Requirements

It is absolutely vital that you come with the required software pre-installed on your PC.

So, make sure you have the following installed on your Mac/Linux machine:

  • Python 3.3
  • Git
  • Firefox
  • Selenium
  • Django >= 1.6

Have a look at the section of the book for the required software installations for more detailed instructions.

Part 1 -- Getting Django set up using a Functional Test

We start by writing a minimal functional test using Selenium, and using it to test that we've got Django installed, and its dev server up and running on our local PC.

We make our initial commit with the Django project and its functional test file. The Testing Goat is introduced.

Part 2 -- Switching to unittest

We convert our FT to using the unittest module, including setUp, tearDown and some of the unittest assertion helper functions.

Part 3 -- Unit tests for a simple front page

Next we start building the front page of our site. We learn how to use the Django test runner, and how to write a unit test which tests a Django view. We do some intitial config in Django's settings.py, and then learn about urls.py and views.py. We learn the feel of the unit test - code cycle, where we write code with small, incremental changes, re-running the tests each time.

Part 4 -- Switch to templates, refactoring, and discuss TDD philosophy

A moment's reflection on what we test and why -- what functional tests are for, how they differ from unit tests, what we should and shouldn't test, and discuss the merits of testing trivial functions.

We then switch our home page view from building HTML out of strings to using a template. We learn how Django templates work, and what "proper" refactoring is. Refactoring Cat is introduced

Part 5 -- Processing user input using forms, POST and database models

We learn how to use Selenium to simulate user input to our site. We start to build up a simple solution to process user submissions using an HTML form, a POST request. We take a few wrong turns along the way, and see how TDD guides us towards the right answer.

Part 6 -- Better design using related models and multiple views.

We fix our broken implementation from the previous section, using incremental, step-by-step changes to the code. We learn how to make changes to the code in such a way that we always go from working state to working state.

We also learn about foreign key relationships, and a simple REST-ish URL structure, with several simple views each doing one job well.

Further topics, if time permits:

  • layout and styling using Bootstrap CSS framework
  • mocks
  • continuous integration (Jenkins introduction)
  • deployment!