Using Test-Driven Development (TDD) in Agile Development

Gita Permatasari Sujatmiko
4 min readMar 21, 2022

--

Image source: Unsplash

This article talks about how we can use the Test-Driven Development (TDD) approach in Agile development and its benefits.

What is TDD and why is it necessary?

In Layman’s terms, Test Driven Development (TDD) is a software development practice that focuses on creating unit test cases before developing the actual code. TDD is an iterative approach that includes three phases:

  1. Develop tests
  2. Write/correct the code
  3. Refactor

In TDD, developers create the test cases first for each functionality of a feature and if the test fails (red), they can then modify or write the code that make the tests become successful (green). Once the tests run successfully, the developers can check if there’s any redundancy or ineffective tests or programmatic implementation (refactor) to improve the performance.

The above explanation tells you that we use the “red-green-refactor” technique to implement TDD.

Image source: thejenkinscomic.wordpress.com

In most cases, developers will write more test cases (unit tests) than the actual code itself. This is because developers are intended to create precise unit tests to test the functionality of every feature based on their assumptions of how the feature will behave. The tests are bound to fail (at first), then the developers can implement the features so that they will function as they were expected to be. Why though??? Why do we need such a process to develop our software?

Here’s a good analogy that I got from reading Test-Driven Development: Extensive Tutorial book by Grzegorz Gałęzowski. Suppose that you have a national exam in five days and your goal is to pass the exam. It would probably pay off a lot more to study chapter by chapter in detail rather than study and exercise from exemplary tests. Those tests are most likely your “benchmark” of the real test. After exercising with the exemplary tests, you now have the specification to pass the real test.

So the tests became something more. They proved very valuable before the “implementation” (i.e. learning for the exam) because:

  1. they helped us focus on what was needed to reach our goal
  2. they brought our attention away from what was not needed to reach our goal

So, writing a “test” is really another way of specifying a requirement or a need for our goal(s), and it occurs in our everyday lives.

Why (should) we use TDD in Agile development?

As I mentioned in my last article, Agile development demands regular feedback to develop a quality product. There’s a high probability that the project requirements may change during the development sprint cycle. With TDD, we can get regular feedback so that we can deliver the products fastly with quality despite the client’s demands of changes.

TDD’s first testing methodology also helped in reducing the significant bottlenecks that hindered software quality and delivery. Systems change in response to constant feedback, bug fixes, and addition of new features ensuring that everything works as planned.

Benefits of TDD

  1. Promotes the development of efficient code
  2. Helps developers to better understand and analyze the client’s requirements and needs
  3. In the later phases of development, adding and testing new features becomes easier
  4. With TDD, test coverage is significantly higher than with conventional development methods. This is because TDD focuses on creating specific tests for each functionality from the beginning
  5. Improves developers’ productivity and leads to the creation of a flexible and easy-to-maintain codebase

Example

Here’s a brief example of how my Software Projects (PPL) course team does our TDD in our Agile development environment.

Unit test example in Python

Above is the unit test for the following functionality.

Implementation code

And here’s how we implement the “red-green-refactor” principles.

Let’s see if I break that down.

First, I created tests for the functionality that I want to implement which is custom pagination.

As you can see in the picture above, the pipeline is still “red” or unsuccessful. That is because it has not passed the unit test that I provided.

Then, I implemented the functionality.

When I successfully implemented the custom pagination functionality, the pipeline finally turned “green”.

After that, one of my teammates gave a review and suggested I refactor my code so that the code can be modular.

Okay, I think that’s it. I hope this article will be useful for us to implement TDD in Agile development. Thank you. 😄

--

--