Test Driven Development (TDD) uses a different approach to that used in traditional software development. In this article, we will find out more about it but firstly, let’s define TDD.
What is TDD?
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.
From this illustration we can define the sequence of steps for TDD as below:
- Write test
- Run test
- Check for failed tests
- Make changes to the code
- Repeat from step 2
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.
Can TDD fail?
Not everyone is convinced by TDD’s perceived merits, with some seeing reasons why it will fail. A few are listed below:
- TDD may not be right for developers who mostly work with a non-TDD approach.
- Not having enough knowledge on unit test framework will also impact TDD approach.
- Complex scenarios require a greater time investment for creating these tests.
- Rapidly changing requirements makes creation and maintenance of these tests difficult.
Common Unit Testing Tools that support TDD
A list of common unit test tools below:
- csUnit : An open source unit test tool that offers a TDD unit test framework for .Net projects
- DocTest: A very simple, easy to learn unit test framework for Python
- JUnit: A Java TDD unit test framework.
- NUnit: This one again is used for .Net projects
- PHPUnit: This one is used for PHP projects
- PyUnit: A standard unit testing framework for Python
- TestNG: A testing framework for Java, which overrides the limitations of JUnit.
- RSpec: A framework for Ruby projects.
In test-driven development (TDD), a test is written first before writing the functional code. This doesn’t mean that TDD can replace traditional QA, but instead it ensures effective unit testing. An effective unit test will ensure better code coverage, and less bugs and QA efforts in the long run. Implementing TDD can be challenging though, because you need people involved who have the necessary skill sets for using different unit test frameworks. If you have the right people, then it’s always good to implement TDD in your projects.