Test Driven Development (TDD) is software development approach in which test cases are developed to specify and validate what the code will do. Test-Driven Development starts with designing and developing tests for every small functionality of an application.
The purpose of Test-Driven development is creating simple and clear code, also minimalize the potential bug in our code.
Benefits of Usign Test-Driven Development Approach
- Better design and higher code quality
- Reduces the time required for project development
- Easier maintenace
- Detailed project documentation
- Constant feedback
- Minimalize potential bug
Stages in Test-Driven Development
In Software Engineering Project Course we are required to use Test-Driven Development approach to develop the product in our project. There are three stages in Test-Driven Development which are: Red, Green, Refactor.
Red: Write a failing tests
In the first stage — the red phase, we should write a test for our future implementation. The test we made shows our expectation on how our code will works. In red phase, the test case will failed.
Green: Write the implementation code to pass the tests
After created a failing test, we should implement our code as we expected it to be. Our test used as our guidance for the code implementation.
Refactor: Write a higher quality code
After passed the test, we are still allowed to modify our code. The purpose of modifying our code is to make a higher quality code for our project. By modifying our code, our test shouldn’t be failed again, it should still pass the test and have a better structure than before.
3 Laws of Test-Driven Development
- Do not write any production code without a failing test first.
- Write only enough test code as is sufficient enough to fail.
- Only implement a minimal code that makes the failing test pass.