Test-driven Development (TDD)

Azzahra Abraara
Portelier
Published in
4 min readOct 18, 2020

In the programming world, there is a software development process named Test-driven Development or in short, TDD. Basically, we have to test our code first before we write our actual code. Still confused? Let’s start to learn it more!

Test-driven development and how it works

Test-driven development (TDD) is a software development which we test our source code first to specify what our code will do. TDD will make us know our code functionality better. TDD consists of test cases, each of the cases tests each of small functionalities in our code later. The flow of TDD is that we create and test our test cases then if the test fails, we start to write our source code based on the functionality that we have already tested before, then we have to test our source code again to pass the test and it will show if there is a bug in our code. Then we can refactor our code if the test passed, if necessary. Refactor is changing or adding the existing code without changing the behaviour of the code. We do the steps iteratively for each of the functionalities. Below is the flow illustration of TDD.

source: https://blog.testlodge.com/what-is-tdd/

There are so many advantages of using TDD, such as:

  • We can detect bugs early and we can refactor it to avoid the bugs reappear in our code
  • It makes our code easy to maintain
  • Saving a lot of time
  • It helps us to understand what will the code do before we implement it

The cycle of TDD

source: https://blog.testlodge.com/what-is-tdd/

RED

this step is when we code our test cases before we implement our actual source code. Each of the test cases represents our code functionality. Make sure that we think a small functionality to make it easy to implement. When we code our test case, think about possible cases for our small functionality’s input and output.

For example in my project, inside my button function, I want to style my button in CustomizedButton function. Then, I create this test.

Then run the test. Make sure that the test fails because we have not write our code yet.

Here I commit my test in gitlab and we can see that my test failed.

GREEN

In this step, we start to write our source code based on our test case before.

Here, in my project, I start to style my button,

and put it inside my Button function.

And begin to run the test again! Here is the example if the test passed.

REFACTOR

In this step, if necessary we can redesign our code to make it more pretty! Make sure that our refactor code does not deviate from the function’s behavior. To make it sure, you can rerun the test, and as long as the test does not fail, you do the refactor well!

Here is the example of my code after it has been refactored. We can see that the test passed.

You can repeat the steps until you feel that all of the functionalities are already tested.

source: https://womanonrails.com/images/tdd-basics/tdd.gif

And, voila! It’s done! That is all for me. I hope you can try to do the TDD process on your project! I hope this article will help you to know more about TDD. Thank you for reading until the end. Good luck! 🥰

source:

http://www.agiledata.org/essays/tdd.html/

--

--