Test Driven Development Obey the test goat

Dec 24, 2020

Test-Driven Development with Python

Continuing with the Test Driven Development books I got started on Test Driven Development with Python and learned to obey the testing goat. As it is an O’Reilly book you can read it online for free. Read TDD with Django here

Part 1

The first part is an introduction to TDD and Django. It starts slow and then ramps up as you get to grasps with the concepts. I recommend Python and Django beginners to first run through the Django tutorial even though as of writing the current Django version is 3.1 and the book uses 1.11. This should not matter too much for the theory of TDD which is the aim of this book.

The books differentiates between functional tests and unit tests as it(functional tests) being the test case which is supported by multiple unit tests. Sometimes people get the two mixed or even use both terms for the same thing. Being able to explain the difference in simple terms and reference to this book could clear things up quickly.

Get quote from book on differences

In the seventh chapter the author switches from showing you what to type to just showing the expected test results. Based on those errors you are supposed to write the code yourself. The code you need to add is a twist on what was added in a previous chapter. I liked this part, especially as it forces you to do it the TDD way by running the tests each line added to check for the correct result.

Sometimes I missed a part and my tests failed when they should not have. Due to the small changes being done I was quick to find which part I missed and could continue on easily. This really helped for motivation to continue because you know you would not get stuck somewhere you could not get out of.

Part 2

While part 1 was more focused on backend development and testing those parts, Part 2 focusses on the web development part. Here an actual frontend is added and tested thoroughly.

Hosting on Google Cloud

After getting a frontend and backend running with unit and functional tests it is time to deploy it to an actual website. As it already had an extra url: vitominheere.xyz, I opted to deploy it there. The book deploys to a staging and production environment, i chose testinggoat.vitominheere.xyz and testinggoat-staging.vitominheere.xyz. As I was familiar with the Google Cloud Platform, I decided I would host it there.

As a server I choose an Compute Engine instance. I still had some free credit left so picked a regular machine type instead of a smaller one. In the end the price did not really matter as I only spun it up while working on the code so it was running for a few hours at a time max. As of now the usage was 2.52 hours which cost €0.05, so don’t expect huge bills just running through this book. Do remember to shut it off though, else the clock will keep ticking.

The only issue I ran into was with the Fabric deployments. You ran them from your local machine and connected to the server via SSH. By default the Compute Engine does not allow plaintext passwords to be send over so I had to change that in the SSH settings.

Javascript testing

The base frontend is just html and some bootstrap. It can be improved by some DOM manipulation, mainly for the error messages. In chapter 16 Jquery and the testing framework Qunit are introduced. As the book is a few years old the current versions of those packages are too new to test with. For the versions in the book I found the files for version 2.0.1 here https://code.jquery.com/qunit/

Part 3

Currently working on it, will update this post when I am done.

Part 3 starts of with a lot of example code for a new feature, logging in via email. The author provides us with all the code necessary to quickly get this running. I did not like this at first but when the tests were created I understood the reason. The author shows us the way of quickly creating a feature, writing tests for it and then merging the tests into the main branch to actually develop the feature in a better way. This way you have the same functionality but the code itself will be better manageable.

Continous Integration is also introduced in this part. You will set up a Jenkins server to run your tests on. Getting this set up in GCP required some effort. I used this article to get it set up. That ensures you are using an Ubuntu VM, which is recommend by the book. You do have to change the command to install Jenkin to the one on the Jenkins website

This part was the hardest for me, had to trace my steps back multiple times. For one exercise I had to look up the source code, fortunately the author has documented everything really well and kept separate branches for every chapter on Github. Each chapter the hand holding gets less and less until one of the final chapters where you are lightly guided through adding a new feature to the site. Here you get some guidance on what tests to write and what to look for, your main guide is the Functional Test that is given to you. I thought this was going to be a hard challenge but the book prepared me well, I followed the TDD principles and wrote tests before actually adding the code for it. It took me a bit of work but I got it working correct in the end.

Part 4

While there is no actual part 4 in the books, there are some appendixes which add things like Class-based views, Ansible and building a REST api. I won’t be working on these extra features continuously but might pick one up when I feel like it or when I need the knowledge for a project.

ProgrammingBooks

Vito Minheere

Book reading list 2021

Test Driven Development By Example