TDD, Is It Matters?

This article written for completing PPL 2021 Individual Review competencies

Rafif Elfazri
5 min readApr 26, 2021

--

Before I take a Software Project course in my college, I think TDD isn’t really important because I thought TDD slows down my progress to create new feature for an app/software that I develop. But I change my perspective when I’m working with a team do develop a software.

What is TDD?

TDD (Test Driven Development) is a software development technique that encourage developer to write test for a feature before implementing it. TDD follows three cycles: RED, GREEN, and REFACTOR.

Source: https://marsner.com/blog/why-test-driven-development-tdd/
  • RED — Write a test for every feature before implementing it, The test should be failed because we haven’t implement any of the features yet.
  • GREEN — Write the code for the features until it pass the test written in RED phase. No needs to worry about readability or efficiency at this phase.
  • REFACTOR — Improve your code efficiency and readability by refactoring your code.
Example implementation RED, GREEN, REFACTOR cycle.

Why is it matters?

Although TDD seems like a lengthy process, TDD offers a huge benefit when applied in a software project. Here are some benefits that I’ve experienced when I applied TDD in my Software Project course.

Easier to debug problem

A well written unit test covers all feature scenario. With that, it helps us to notice what scenario that has bugs or error in code implementation that we write and points out were the part of your code that has a problem. It saves times for you from searching the bugs or error by testing a function or a feature manually.

Enforce you to think before code

TDD cycle enforce developer must makes a test for a feature first before implementing it. With that, developer must think every scenario that would happens in a feature an express it in a test. The test will guide developer to write code that satisfy every scenario that written in the test.

Makes you code better

TDD test makes give a warning to your code if your code have an error or logic error, this could save your code from an unwanted errors or bugs before deploying it to production. TDD also encourage refactoring your code in REFACTOR phase, it encourages you not only to write implementation code that barely pass the test, it also encourage you to write a better code that more efficient and easier to read by other developer.

How to make good test in TDD

Sometimes developer have a hard time to write a good test before implementing the code and usually takes approach to DDT (Development Driven Test) which a cycle that you write the implementation first and write a test according to your implementation code. Here are my tips to create a good test.

Thinks all scenario of your feature

As I said before, a well written test is a test that covers all scenario of your feature. You could use use a help of a flowchart to help you to write your feature scenario.

Flowchart Example (Source: https://www.visual-paradigm.com/tutorials/flowchart-tutorial/ )

Flowchart helps you to write code by its branches. You could identify every scenario of you feature easily by identifying the flowchart branches of your feature. After this, you could write your test based on scenario that you get from the flowchart.

Check the needs of isolation in your test.

Sometimes, developer have a difficulty to write a test because the implementation relies on stochastic result or the function used in the implementation is non-deterministic (Example: Oauth API). Developer could write a mock function to control the output of that function to isolate your test from any uncertainty.

Example test with mocking

Aim for high code coverage

Code coverage is a percentage of how many lines of code that tested in your test from your whole code. Code coverage is a good metrics of how your test covers all of your scenario. Although this metric could be cheated by writing a test that didn’t test any behavior or output from your code, you mus not do it because it don’t bring any benefit in your project and it could hinder your project because it could hide logic error from your code.

TDD Example

supposed we need to build a module that could do reguler auth by backend and google Oauth. First we need to specify think the flow of the module.

Above are the flow chart to help us to create the scenario. The google Oauth validate token by sending a request to google server, So we need to build mock function for the test. Below are the test for the module.

and here are the implementation of the code.

Conclusion

TDD is an important practice to follow in developing a software. Although it seems like a waste of time, TDD offers great benefit to your software development project as it helps developer to think before code, makes developer codes better, save developer time in searching of an error or bugs. I hope this article could helps you to implement TDD practice in your software project and helps you to write a better test.

References

--

--