What is Test Driven Development (TDD)?

10 Feb 2024
8 minute read
TDD is a software development approach that involves writing tests before writing the code. Test cases are derived from the business requirements to validate the intended behavior of the code.

However, many people view TDD as a difficult and time-consuming approach to adopt, which can create a mental barrier since it requires stepping outside of one’s comfort zone.

Simplifying Testing and Improving Efficiency

Suppose you are developing an API endpoint, you will need to test it using tools like Postman or other REST clients. To do so, you will create a request payload and verify the API functionality by testing it with various requests.

In case you modify the API implementation or added new validation rules, you need to manually test and confirm the changes. However, there’s a possibility that you may miss reviewing the API response.

So you’re already investing your time to construct the request payload and testing it in the tools like a postman. TDD encourages you to do this in a more concise and organized way so that regression will become headache free.

Since you are already spending time creating the request payload and testing it with tools like Postman, TDD promotes doing this in a more efficient and structured manner, which can make regression testing easier and more convenient.

The three-step process (Fail, Pass, and Refactor)

The Mantra of TDD is expressed in the phrase “Fail, Pass, and Refactor.” It means that initially, the test will fail, and you should make enough changes to the code to pass the test. Then, you can refactor the code to improve its quality and maintainability.

Out-of-the-box thinking 🤔

Most of us have heard about the S.O.L.I.D. principle. If the single responsibility principle is applied correctly, it can be very helpful when writing tests. This is because functions and methods will have only one responsibility, It’s a challenge for you guys to apply the rest of the principles 🙂

Types of TDD

There are usually three categories of tests involved in TDD

Unit Tests
The unit tests are relatively small and specifically designed to validate the behavior of individual functions or methods. They are executed in isolation from the rest of the system. Unit tests are written before the code is implemented, and they help ensure that each piece of code works correctly on its own.

Integration Tests
The integration tests help us to verify how different components of the system work together. Integration tests are written after the code is implemented, and they help ensure that the various parts of the system work together correctly.

Acceptance Tests
The acceptance tests help us verify the overall behavior of the system from the perspective of the end user. Acceptance tests are also written after the code is implemented, and they help ensure that the system as a whole meets the requirements and behaves as expected.