Test Driven Development

A development process where implementing tests comes first!

Muhammad Irfan Junaidi
4 min readMay 12, 2022
TDD by MonkeyUser (Source)

It is quite common that you need to test your software before going to production. But how about testing the software before finishing or even implementing the code? Though it may sound weird to some people, Test Driven Development (TDD) have been used by many developers from time to time as developers tend to produce fewer bugs. It also may cause a slow development process at the start, the need to maintain tests, and may be difficult to do for people who’s just starting out.

So how do you implement TDD exactly?

TDD follows the concept “RED, GREEN, REFACTOR”, RED means writing a test that fails, GREEN means implementing the code that pass the test, and REFACTOR means cleaning your code without changing its implementation. In the RED phase, developers creates the tests for the code that will be implemented. For best practices, it is recommended to mark the commits with the current phase, in this case, I usually mark it as “[RED] My Commit”.

After creating the tests, you can start implementing the codes so that the code passes tests you’ve made before. Once you’re finished implementing the codes and passed the tests, you can commit your work with the tag [GREEN] as a mark that the feature you implemented runs correctly based on the tests.

Sometimes, you don’t always add new features to your software. Instead, you try to improve them by using better performing codes or even cleaning some of the smelly codes that you’ve wrote. By doing any of those mentioned before, you might want to label your commits with the tag [REFACTOR] to show that you refactored the code base. Do note that it still counts as refactoring as long as the behavior of the code stays the same. To know more about Refactoring, you can read my article about the topic.

What are the benefits for using TDD?

Of course people won’t use certain methods if it has no gain for them, here are the benefits of using TDD.

  1. Modular Software Design, by using TDD, developers are forced to focus on a single feature at a time until the tests for that feature has passed, making it easier to discover bugs since the codes corresponds to certain test.
  2. Easier to Maintain Code, developers tend to produce a more cleaner, more readable, and more manageable code due to TDD. Because of focusing on smaller code fragments, it requires less effort for developers to maintain the code.
  3. Better Code Documentation, the unit tests that are created acts as the documentation and describes how the code should work. This way, it is easier for developers to understand the code even without detailed documentation.

Though some of you may think that TDD seems to be flawless at this point, every approach has its ups and downs, including TDD.

The drawback of TDD

  1. Slow Development Process, like most romcom manga, the development process tends to be slow, especially in the beginning. The implementation of the codde may not be ready on time since the developers will be writing tests before any implementation. Though in cases where the software development takes up to double digits sprints, TDD may be faster than your normal development process.
  2. Difficult to Learn, TDD may be quite challenging to learn and requires skill, dedication, as well as discipline because the method is quite different where people usually starts. It is also difficult to do if you’re new to a certain framework as you might not know yet how to implement some features, thus unable to make the tests.
  3. Committed Team, involvement of the entire team is crucial in TDD. Everyone needs to believe in the importance of the TDD process, even when the team needs to re-write code because of the change in requirements.

My team’s TDD process

Here is an example of how my team uses TDD in our project

Commit History in our project

The picture above shows how we mark commits based on the phase we’re currently working. The commit with the RED tag shows that the test fails, while the one with the GREEN tag passed the tests.

Here’s a picture of the test I made on the RED marked commit above

And here’s a picture of the implementation of the test

Should we implement TDD?

In my opinion, aside from Agile, TDD really tidies how my team develop software. Though it may not be faster, it really has a positive impact on the software we’re working on. But, depending on your project, you may want to discuss it with your team whether all of you want to commit in using TDD.

References:

--

--