Test Driven Development (Beck, 2003) is an approach which incorporates Test First development (TFD) as the name suggests is the practice of writing tests before functional code is produced. TDD is normally implemented at the unit or component testing level. However, the main difference between TDD and TFD is that TDD normally includes the agile practice of refactoring which implies that TDD is an important agile requirement and design. In the world of Agile TDD is helping many organizations improve their software thus ensuring their software is robust.
Test Driven Development Cycle
Add a Test
The first step is to write the test for the feature that needs to be implemented. To be able to write the test the developer must understand the requirements for that specific feature. The importance of writing the test first ensures the developer is focused on the requirements and design before any functional code is written.
Watch Test Fail
The importance of watching the test fail is that the developer can be confident that the new test written is isolated and doesn’t rely on old code. This shows that there are not any flaws in the test and the behaviour of the new test is as expected.
Write the Code
At this point the code written will not be completely perfect as the developer should only be writing code with the functionality to pass the test just written.
The developer can now run all test cases at this stage the developer should have confidence that all tests will pass and the new code written doesn’t break any existing tests.
The code written can now be clean up and place in a logically position as suppose to just being place any where for the sake of passing the test. Any duplication must be removed. In addition, methods and objects can become larger; likewise, can be split to improve code readability and maintainability.
The process can now be repeated again for new features.
The image (Bryan, 2015) below illustrates how TDD should be implemented.
As aforementioned TDD allows developers time to think thus giving them a focus on what actually needs to be implemented. Moreover, it enables developers to implement code in small steps rather than writing large piece of code which tend to have various bugs.
TDD also has a number of benefits:
- Little documentation of the code is needed as the unit tests will document the expect behaviour of the code.
- Unit test can be ran at any time and automated.
- Due to the speed of the test-code cycle, refactoring can be done at a fast pace.
- To be able to test code in isolation good design principles must be understood.