TDD is an innovative software development approach where tests are written, before writing the bare minimum of code required for the test to be fulfilled. The code will then be refactored, as often as necessary, in order to pass the test, with the process being repeated for each piece of functionality. The below illustration shows how it works. To summarise, write minimal code and refactor continuously in order to satisfy the test.
Test-Driven Development starts with designing and developing tests for every small functionality of an application. In TDD approach, first the test is developed which specifies and validates what the code will do.
How to perform TDD Test:
From this illustration we can define the sequence of steps for TDD as below:
Check for failed tests
Make changes to the code
Repeat from step 2
Some clarifications about TDD:
TDD is neither about “Testing” nor about “Design”.
TDD does not mean “write some of the tests, then build a system that passes the tests.
TDD does not mean “do lots of Testing.”
Test-Driven development is a process of developing and running automated test before actual development of the application. Hence, TDD sometimes also called as Test First Development.
Advantage of TDD over the Traditional Approach
In the traditional development approach first, you select a user story, then develop it, run tests over it and refactor the code until the test is passed. In this method, the coding is done before testing. The disadvantage of using this approach is that you are unable to discover the errors or missing cases until the functionality has been fully developed. With TDD, you write tests upfront for functions that doesn’t yet exist. You know that the test will certainly fail at the start, but minimal incremental coding at each stage will make sure it will eventually pass at some point. So, this process makes sure you won’t miss any scenarios once the functionality is fully finished.
Benefits of TDD
Software development teams have benefited a lot from TDD. Here are a few reasons why:
With TDD, tests will be automated, saving a lot of time compared to manually testing functionality.
With TDD, tests are created for each bug. These tests will be run each time there is another bug fix. This is to make sure bugs don’t reoccur in the code.
Because tests are in place upfront, regression testing time can be reduced considerably.
Tests written ahead of time will also ensure good code quality.
Writing tests, followed by minimum code changes after each test run will make sure there is good unit test coverage for the software, which will again contribute to the overall quality of the product.