Embracing Test-Driven Development
A Not-so-simple explanation of TDD
“Do The Simplest Thing That Could Possibly Work”
― Kent Beck
Sometimes when we are trying to write down complicated business logic into code, it takes us several hours to validate if the implementation is under the requirement. Furthermore, if we find out that there are bugs in our code and we don’t exactly know how can it happen. These are some frustrating situation you may want to consider Test-Driven Development.
What is Test-Driven Development?
Test-Driven Development (TDD) is a practical method that guides software engineers to write code implementation based on the test that they have created before. The term “Test-Driven Development” dates back to the 1990s as it was part of Extreme Programming, a software development methodology proposed by Kent Beck.
The main idea of TDD is simple. You can only start writing the code implementation after you write the test and only on the scope of the test. Then, after you are sure that there is no error occurred in the test set, you are allowed to write another test. Therefore you are aware and in complete control of the current state of your program.
How to implement it?
As we already know, We need to write the test case first before implementing the code. There are three main phases for conducting Test-Driven Development. Those steps are writing a failed test case, implementing the code to pass the test, and refining or refactoring the code. We repeat the phases until the program reached our goal. Look at the graphic below to understand TDD better.
1. Write Failed Test Case [RED]
In this first phase, your primary goal as a software engineer is to write a specific test case for certain functionality or feature. The produced test case must be able to get compiled by software engineers and fail the first time it runs. The failed test case’s purpose is to give you a sense of how the implementation works in the future.
What if the test case itself is confusing? There is a concept to help software engineers to write clean codes. That concept is F.I.R.S.T. It is an acronym that stands for Fast, Independent, Repeatable, Self-validating, Timely. The explanation of each principle is explained below:
Test suites (collection of test cases) must be able to run quickly. The idea is that so software engineers are not disinclined to execute the test suites.
Each test case in a test suite must not be dependent on another test case. If the test cases are so reliant on each other, one failed test could cause a series of failures in the test suite.
The test cases must be able to run in any environment They must be able to run in your local environment, production, QA, etc. If they don’t have that ability, you will not be able to run test every time you want.
Every test case must only have two options of outputs, either pass or fail. The goal is to prevent subjective errors in the failure state of a test case. Subjective errors occur because of manual testing.
Every test case must be available before the production code so that the results of the production code are testable.
2. Implement The Code To Pass The Test Case [GREEN]
This phase is your time to develop the implementation. The implementation must be as minimum as possible just to pass the test case you created. Make sure you don’t develop an overkill implementation to prevent missed code part to be tested.
3. Refined your code [REFACTOR]
The last phase is for you to make your implementation cleaner. Your goal is to reduce duplication and make sure your code is readable so that other software engineers could comprehend it easily.
The Great Benefits of TDD
- The end result of Test-Driven Development is a maintainable code. Your code will be easier to read, clean, and manage at the same time. It correlates with the clean code concept.
- The cost of project will be decreased. Since TDD will result maintainable code, you will have less money spend on bug fixing.
But, There are also drawbacks..
- You maybe understand the concept of TDD but when you learned a new programming language, TDD concept is beyond understanding to implement. You must be able to utilize programming language first before you implement TDD.
- It will require more time to develop implementation. Since you need to make test case first before you can write the implementation, you will find your time to code is more than usual.
Introduction to Test Driven Development (TDD)
Test-driven development (TDD) ( Beck 2003; Astels 2003), is an evolutionary approach to development which combines…