Introduction to Test-Driven Development

Rayhan Muzakki
Pilar 2020
Published in
3 min readOct 22, 2020

One of the worst experience in coding, debugging. Especially if your code consists of hundreds of lines, you have to check every line in your code to know which part needs to change so that your program works as intended.Fortunately there is a way to avoid getting a bug and that is Test-Driven Development(also known as TDD).

What is TDD?

TDD is a software development process in which requirements are turned into a test case and write codes so that the test passed. There are 3 stages to determine which process should be taken: RED, GREEN and REFACTOR.

Stages of TDD

RED

Red stage is the stage where we write tests with their requirements and purposely failed the test. The test should be simple and focus on 1 function of the requirement,thus 1 feature can have multiples of tests, writing tests first could give a goal to you on what to do.In Git the stage will be displayed in the commit by writing ”[RED] comment written here” and it will be shown as progress.

Example of a test case, source: Pilar Project
Example of Git TDD

GREEN

Green stage is the stage where you will be writing codes in your main program to pass the tests that was made, writing the codes should be only to pass the test and not add any lines that are not correlated to the tests. After passing the tests it is ready to move to the next stage, in Git it will be displayed as “[GREEN] expecting comment” showing that the specific test has passed.

Example of code to pass the test, source:Pilar Project
Example of Git TDD

REFACTOR

During this stage you are able to do minor changes to your code, minor changes mean applying clean code and not changing your code so that it fails.This is to ensure your code is flexible and simple, in Git it will be displayed as “[REFACTOR] expected comment” showing the minor changes applied.

Example of Git TDD

--

--