Test-Driven Development for Better Software

Yafi Ahsan Hakim
Portelier
Published in
4 min readOct 22, 2020

As a programmer, you might have heard the term test-driven development before, but do you know what is it exactly and how to actually do it? Well, this article here will help you understand what actually is test-driven development.

source: https://brainhub.eu/blog/wp-content/uploads/2018/12/test-driven-development-tdd-cycle.jpg

What is Test-Driven Development (TDD)?

Test Driven-Development or TDD is an approach in software development in which test cases are made first in order to validate what our code will do. Basically, in TDD, we need to create test cases before we do any code writing, test cases for each part of our code will be made first and then tested, if the test fails then the new code is then written in order to pass the test. the main goal of TDD is to avoid having a bug in the final source code by having every part of the code tested.

How to do TDD?

In practice, TDD is divided into three steps or phases, which is Red, Green, and Refactor phase.

Red

The red phase is the starting point of our red, green, refactor cycle. In this phase, what we do is we write a test based on what we want the code does or what is the functionality of the code. When we run the test, it will fail and this is exactly what we want in the red phase.

for example, in my project, I want to create a new component named PictureLayout, so I added a new test which renders my component and then run the test, which ends in a fail.

Green

Next cycle is the green phase, in this phase, we start to write just enough code to pass the test that we created in the red phase. Keep in mind, in this phase, we only need to make sure that the code passes the test, it’s okay if our code is not clean or have any duplicate code because the purpose of green phase is only to pass the test.

for example, in my project I already created a test for my component, now in green phase, I added the component I want to add until the test is passed.

Refactor

The refactor phase is where we are allowed to change our implementation while still keeping all the tests green. If in the green phase our code still has duplicate codes, this is the time to remove those duplicate codes and improve our implementation. The most important thing in this phase is to keep the tests green, it doesn't matter if our implementation is the most optimal one if the test is failed when it is run.

After you have done the refactoring, one cycle of red, green, and refactor is done! Next thing you need to do is repeat, repeat, and repeat until your program is working as you intended it to be.

Benefits of TDD

Using TDD as our approach to creating our program has a lot of benefits, one of them is it helps us to understand how the code will be used and how it interacts with other parts of our code and it results in a more maintainable code. Other than that, using TDD could help us in early bug notification, because we create a test for each small portion of the code, when we found a bug we will need to resolve it before moving on to write more codes. Although it is true that the developers have to spend more time writing the test cases, it will lessen the chance that a huge bug will be present in the final product. it is better to spend more time debugging many small bugs in the middle of writing the program than debugging a huge bug at the end of the production when the program is already finished.

--

--