What is TDD (Test-Driven Development)?
Before we discuss what Test-Driven Development is, let’s first look at the history of testing. The concept of testing before implementing features was introduced by Kent Beck in 1996 through the Extreme Programming methodology. This practice of “test-first programming” later became known as Test-Driven Development (TDD).
Test-Driven Development (TDD) is an approach used in software development where programmers interleave the process of testing and code development. Basically, programmers build code incrementally alongside quality assurance (QA) for each code segment.

Programmers only write the code strictly necessary for the feature, writing as little code as possible with a primary focus on passing the tests. TDD provides many benefits in terms of increasing system productivity and code quality. Therefore, TDD is widely used by software companies that apply Scrum or Agile working models.
The TDD workflow consists of three parts: Red, Green, and Refactor, as shown in the lifecycle diagram above.
Red
The first step in applying TDD is writing the test flow. At this stage, the test will certainly fail because the feature or code has not yet been created. This is why it’s called “writing the test” in the diagram above.
It’s normal to write code that fails initially; this code will be adjusted during the development of functions and features in the application.
Green
After writing the failing test, the next step is writing the code to satisfy the test scenario. The main focus here is writing just enough code to pass the test.
However, it’s important to keep in mind that passing the test isn’t everything. We also need to consider cause and effect that might lead to compilation failures, so careful coding for testing is required.
Refactor
When initial code is written quickly just to make a test pass, it might not be the most efficient or clean. In the Refactor stage, we improve the code to make it more efficient, readable, and maintainable by other developers who review our Test-Driven Development work.
The refactoring phase is very helpful in maintaining a clean codebase while building applications. Therefore, we need to refactor to improve code quality. In essence, make the code as good as possible.