The Benefits of Using TDD
Facundo Revello
April 21, 2020
Test-driven development (or TDD) is not a new concept; for years programmers had the idea of testing before programming. However, TDD as a concept and a technique has become more popular and standardized thanks to the introduction of Agile methodology. In this post, we’ll explain the benefits of using TDD and show you how it works step by step.
What is TDD?
First of all, we have to understand that TDD is not the same as unit testing. TDD is a programming technique that uses unit testing. The main idea of TDD is that before writing any code for our system, we must first create the tests for that code. At first this might sound a bit strange, since in traditional techniques the tests are created after the code is written. However, as the name "test-driven development" implies, the test will guide the writing of the code. This allows developers to focus on the requirements before writing the code.
Why Use TDD
TDD provides several benefits for developers when it's done right:
-
TDD gives developers more confidence in the code that they are writing, as the tests will fail or succeed automatically.
-
The developers can imagine the functionality while creating the step, allowing them to gain more insights on the features they are building.
-
The number of bugs reported in later stages of development are reduced considerably. The cost of fixing a bug grows exponentially as the development process advances, so TDD also significantly reduces the cost of bug fixes.
-
TDD results in a more reusable and modular system that will be prepared for future changes.
-
TDD helps ensure that the system meets the defined requirements. This is crucial to meeting client expectations.
-
When using TDD, the test coverage is 100%.
How to Use TDD
In order to use TDD there are five steps to follow. These steps are commonly known as red, green and then refactor.
Create a test.
Before writing a new feature or functionality, the test for that code must be created. However, before creating this test, the developer must have all necessary information and knowledge about this new feature so they can create the test properly. The test should be short and fail when running the code.
Run all tests.
This step validates that the new tests that were added are working correctly. If a new test fails and the already existing tests work as expected, we can confirm that the new test was added correctly. We refer to this step as the red one, as this color represents the failing test.
Write minimal code.
The next step is to write the minimum amount of code to pass the test. The code written in this step doesn't need to be perfect and can be improved later, but we'll handle this in the last step. The main idea of this step is for the code to pass the step, so the developer should avoid writing unnecessary code that exceeds the needs of the test.
Run all tests.
Repeat the second step, but this time all the tests should pass. This will be an indicator of how the new code and tests are working and whether they are affecting the existing code and tests. We refer to this step as the green one, as this color represents the success of the tests.
Refactor the code.
The last step involves all the improvements and changes that need to be made to the new code. The general idea of the code refactor is to review the code, so changes can be identified and improvements made.This might include cleaning the code, checking method and attribute names. Checking the length of methods and functions, splitting longer methods into smaller ones, or moving the code to a more appropriate section of the system. It is advisable to use the clean code best practices when refactoring the code.
Repeat the cycle.
After completing the refactor step, we can repeat the whole cycle by creating new tests in order to make progress in the implementation of the functionality or feature. It's important to take into account that if the new code breaks existing tests that were previously successful, we may need to consider reverting this code.
TDD is a great programming technique that allows developers to improve their performance and confidence. TDD is a great way to make sure the product fulfills the client requirements, and we hope our post can help developers understand the basics.
How We Do Code Reviews
Thorough code reviews are an important part of how we deliver state-of-the-art web and mobile apps that users love. In this post, we walk through our process step by step.
The Scrum Pillars at Sophilabs: Inspection
Our teams are committed to practicing Scrum. In this second installment of our Scrum Pillars series, we look at how we practice inspection in every sprint.
Join our team
If you're passionate about building quality software and our values resonate with you, get in touch with us!