We, as Developers, are familiar with Testing or Unit Testing. Unit Testing is a method to ensure individual units or components of the software are test. The purpose is to validate that each unit of the software code performs as expected. Unit Testing is done during the development (coding phase) of an application by the developers. The workflow of writing unit testing is after we implement the Code or feature.

This sometimes tends to make developers think the same thing twice, which are when implementing the Code and when writing unit tests. Also, if the implemented Code is a complex feature, the Developer would find it difficult to write test cases and would miss some scenarios of a feature.

What if we reverse the workflow, write the test first, and implement the Code later? This comes with the approach of Test Driven Development.

what is Test Driven Development?

Test Driven Development (TDD) is a software development approach in which test cases are develop to specify and validate what the Code will do. In simple terms, test cases for each functionality are create and test first, and if the test fails, then the new Code is written in order to pass the test and make the Code simple and bug-free. three main Stages Test Driven Development:

three main Stages Test Driven Development

Red

Red think about what you want to develop. As the Code is implement later, when we write a test case, it will fail. This is the Red phase. In this phase, we should write the test that matches business requirements and ensure some scenarios are cover.

Green

Green think about how to make your tests pass. After we write a test case and run it, then we move to the Green phase, which is simply to make the test pass, and in this stage, we don’t need to over-engineer the implementation; the goal is just to make the test pass or green with simple implementation.

Refactor

Refactor think about how to improve your existing implementation. You can begin thinking about how to implement your code better or more efficiently. Then when it needs improvement, we can refactor the Code as long as it follows the requirements / test should be pass. When we need to improve a Test Case with new or updated requirements, we continue the loop to the Red Stage, where test cases would fail again. And then make implementation and refactor later. At the first time, you will find TDD is challenging, as you need to figure out what the behavior of the feature or implementation could be in your mind.



In this session, you saw an example of how you can use Test Driven Development to improve confidence that your code is working as expected. You also saw how to use an existing test to keep you on track while you refactor implementation code. If you use red, green, and refactor Test Driven Development to approach large software implementation problems, you will find yourself saving time, while implementing a more robust solution than you otherwise would have. check others article in here.