You will learn what is Test-Driven Development or TDD approach in this post.
Traditionally, most of the legacy systems had been developed first and tested later for the expected behavior as per the requirements. This approach left many execution paths are untested and prone to bugs to be hidden within the systems.
Many of these bugs have been found during the testing phase, and some are in the production with unexpected crashes or issues. Increased technical debt like this left those applications with less maintainability, increased the cost of maintenance, and complicated to add any new features to enhance the system.
Nowadays, with the expectation of continuous delivery with high quality forces developers to embed the automated testing within their development phase. This testing process ensures that more bugs are captured while the functionality is being developed so that they could be fixed along with the feature.
With the popularity and broad adaptation of agile development methodology and pair programming, tests are being written before the actual functionality being developed to ensure that the system will be tested against the expected specification for the business requirement.
More tests are supposed to be prepared to make sure the systems’ conformity to design specification and a robust system’s delivery to the end-users.
The approach of writing the tests first and then the confirming or passing functionality code is called Test Driven Development (TDD). This blog post’s primary goal is to discuss the Test Driven Development approach.
We will go through what is TDD, its benefits and how it is integrated with the development process.
What Is TDD?
Let’s see what the Test-Driven Development (TDD) method is. It’s a development approach where the developer writes the small test for functionality and then writing the code for that feature to pass the previously written test and then refactor the code without changing the behavior.
Then repeat the steps with more enhanced tests until both the tests and codes are satisfying the functional specification of a requirement.
The small test that was written is called Unit Test. To test a small unit of code, we usually write a unit test. Without any dependency, the single unit of code can be tested with the help of a unit test. In the TDD approach, we need to write the unit test for the functionality before the actual code.
Here are steps that we need to follow while employing the TDD approach.
- Step 1: Write a partial unit test.
- Step 2: Run the unit test to fail it (As we haven’t written the actual code yet).
- Step 3: Write the minimal code for the functionality to pass the partial unit test.
- Step 4: Run the unit test,
- Step 4a: If it passes, refactor the code without changing the behavior and without failing the test again.
- Step 4b: If it fails, go back to
Step3 to write/ modify the code minimally to pass the unit test.
- Step 5: Repeat Step 1 with the more enhanced unit test for that functionality
TDD Process Flow
Here is the process flow diagram of the Test Driven Development approach.
Why Is TDD Needed?
Writing the tests during the development phase would be time-consuming, and the developer needs to have the required skill set to do so, what are the benefits we could get out of TDD?
Benefits of TDD
There are many benefits of using TDD in the day-to-day development process. Below are few of those.
- The quality code will be developed which conforms to the requirements specifications.
- Development time to be faster with the TDD approach over time.
- Maintainability of the code base will be easy as there will be tests for the existing codebase. This will make sure that new changes affecting the current features will be captured at the earliest. This way those failing code will be fixed along with the new changes.
- The unit tests used in test-driven development will be acting as the regression test suites for the whole codebase. So that any breaking changes will be captured during the development phase.
- All the unit tests will be acting as live executable documentation of the codebase that we maintain as these test cases will always be running in every development cycle to make sure that all the components are working as designed.
- Regularly, the existing code base might be cleaned up with the refactoring happening at any new feature additions are enhancements, this will ensure that the technical debt will be reduced and the code will be cleaner over time.
- Debugging time will be reduced as the unit test it isolated and tests the small portion of the codebase, it will be quicker to debug to find the bug and fix it.
Drawbacks of TDD
As there are many benefits we can get out of the TDD approach, there are few drawbacks as well.
- The organization needs to take into account the initial ramp-up time is necessary for the developers to do TDD as it has a learning curve to adapt to daily development activities.
- As a developer, they need to take a different approach from the traditional development approach as the TDD will force them to think of better software design and this will require time and effort to adapt.
Please check out the index page for this Node.js RESTful API development with TDD approach tutorial with all the posts in sequence in one place.
This blog post is an excerpt from the book Building Node.js REST API with TDD approach. Please check out the link for more information.