Home of a Creative Engineer

Test Driven Development – A practical Example

In this article I demonstrate the principles of Test Driven Development on a real project. I will guide you step by step through the development of a working application.

The Project

The application I am going to develop in this example is a calculator that can be used on the command line.

The application is written in Python 3.6. I use the Python unittest framework and the Python mock framework. Both are shipped with Python (if you have at least version 3.3), so you don’t have to install them yourself.

Step One

At the beginning of the development session, we start with a list of functionalities that we want to implement. Using the list, we make sure that we won’t forget any task. And it helps us to focus on one task at a time because we don’t have to think about the other tasks. During the development, we will cross the tasks off, one after the other. It can also grow over time as we find more aspects that need to be handled.

This is the list of tasks that come to mind at the beginning of the session:

  • Empty Input
  • Invalid Input
  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Bracketed Expressions

As we start the project from scratch, we want to give it some structure now. In order to make progress in that direction, let’s choose the simplest task from the list: handling an empty input.

The test shows the first steps that we are going take:

  1. We want to create a class CalculatorApplication. It will act as the central object of the application, which will be used to perform all calculations.
  2. The class CalculatorApplication shall have a method calculate that will take a string with the expression to calculate and return the output as a string.
  3. If the input is empty, then the value “0” shall be returned.
  4. The test fixture class TestCalculatorApplication will group all tests for CalculatorApplication.

To run the test from the command line, we can use the following command. It scans for files in the directory that contain test fixtures – classes that start with Test and are derived from unittest.TestCase. Then it executes the tests in these fixtures.

The test fails. The error message tells us that the class CalculatorApplication is not defined:

At this time, we are in the first phase of the TDD cycle. We have written a new test but it is failing. It is red.

Let’s fix that by defining the class CalculatorApplication and giving it a method calculate, using a dummy implementation:

Let’s execute the test again. It still fails. But the error message is different:

This new error message shows us that the test works correctly – The method calculate returns a value that is not expected by the test, and that is why the test fails.

Let’s make the test green by changing the dummy implementation to return the correct value:

The test passes:

This completes the second phase – the test is now green. We will skip the third phase – refactor – because there is nothing to refactor yet.

Summary of Step One

The first test is in place. This seems like a tiny step, but is has brought us some progress:

  • We know that our toolchain works.
  • We have our first class that we can extend later.
  • We have a first realistic test case that will prevent us from introducing errors during later refactorings.

We can now check the item “Empty Input” in our list.

 

This article continues on the next page


I hope you enjoyed this article. If you have any questions or comments, please contact me.