What is TDD (Test Driven Development)? A Complete Guide with Examples

Arrow down icon
Published on
February 20, 2024
Boris Šijan
Published by
Boris Šijan

In this article, we are going to give a brief introduction to the Test Driven Development (TDD) methodology. We will see how TDD works and how it is applied in software development, as well as its advantages and disadvantages.

What is test-driven development?

Test-driven development is a software development methodology in which the developer writes tests before writing the code for a new feature.

This approach can simplify development and ensure that code meets requirements. TDD allows developers to focus on individual requirements and ensure that code meets those requirements. This approach also simplifies development, as you can write code to meet specific tests.

Test-driven development is an iterative process. In each iteration, you write unit tests for a specific requirement. You then use those tests to drive code development to meet that requirement. You repeat this process until all requirements are met.

In unit testing, a unit is the fundamental testable part of an application. It is a cohesive part of the application that represents the main goal/functionality of the program. Unit tests validate these units by supplying various inputs and verifying outputs. If the output does not match what was expected, it is considered a failed automated test.

TDD pros and cons

One of the main advantages of TDD is that it helps to develop clean and well-designed code. By writing tests before code, you think about how the functionality should be used and design your code in a way that is easy to test. In addition, by having automated tests, errors and defects in the code can be detected early, which makes it easier to correct them.

TDD also helps improve code quality by making it easier to refactor. By having automated tests, you can make code changes with confidence, knowing that the tests will detect if any existing functionality is broken.

However, TDD can be tedious and requires more time to write automated tests for every small change in the code. Also, it can be difficult to write tests for complex functionality. Therefore, it is recommended to use TDD only for critical functionality and to use other testing techniques for less critical functionality.

In general, TDD is a useful software development approach for improving code quality, detecting bugs early, and facilitating the refactoring process. However, it takes a bit more time and effort to write automated tests.

«Red, Green, Refactor»

TDD is divided into three steps:

  • write an automated test,
  • write the code needed to make the test pass
  • and refactor the code to improve its quality.

This cycle is repeated until all the required features are implemented and pass the tests.

The TDD cycle is known as “Red, Green, Refactor” because of the colors used to indicate the status of tests. The loop is started by writing an automated test for a specific functionality. In this step, the test is expected to fail ( red ) since the functionality has not been coded yet.

After the test is written, the code necessary to make the test pass is written. In this step, the code is expected to pass the test ( green ) and is verified that it meets the requirements specified in the test.

Finally, the code is refactored to improve its quality, ensuring that the tests continue to pass ( Refactor). The cycle is repeated until all the required functionalities are implemented and pass the tests.

In short “Green, Red, Refactor” represents the sequence of first writing an automated test, then writing the code needed to make the test pass, and finally refactoring the code to improve its quality and ensure that the tests continue to pass.

When to use TDD?

There are some situations where it is highly recommended to use TDD:

  1. Projects with changing requirements : TDD helps to detect changes in requirements early and to ensure that the code continues to comply with them.
  2. Projects with a high risk of failures : TDD helps to detect and correct errors and defects early, which reduces the risk of system failures.
  3. Projects with a high degree of complexity : TDD helps ensure that code is being built in an incremental and controllable manner, making it easier to manage complex projects.
  4. Projects with a decentralized team : TDD helps ensure that all team members understand the requirements and that the code complies with them.

In general, TDD is a methodology that can be beneficial for any software development project, but it is especially useful in projects with a high risk of failure, a high degree of complexity, or a decentralized team.

TDD example in C# .NET

Here is an example of TDD in C# .NET of a functionality of a class of a web application:

1. Write an automated test for a specific functionality. For example, if we want to test a class that validates if a user is an administrator or not. We could write a test like this:

2. Write the code necessary to make the test pass. In this example, we would write the implementation of the UserValidator class:

3. Run the tests and make sure they pass. In this case, the test should pass since the user has an administrator role.

4. Refactor the code to improve its quality, ensuring that the tests continue to pass.

In this example you can see how it is verifying if a user is an administrator or not, through the UserValidator class, more tests can be added for different cases and scenarios, such as a user that is not an administrator or a user that does not exist.

In this way, functionalities and automated tests can be added for each of them, guaranteeing that the code meets the specified requirements and detecting errors and defects early.

Conclusion

In short, TDD will help us ensure code meets specific requirements and detect bugs and defects early by writing automated tests before writing system code. It is especially useful in projects with a high risk of failure, complexity, and decentralized teams.

Feel free to contact us

Enlight logo