Test-Driven Development

April 1, 2016

TDD 3-Steps

What is Test-Driven Development (TDD)?

Test-driven development (TDD) is a software development process that uses repetition of tests and short development cycles. First, we write an automated test case (initially failing) that defines an improvement or new feature, then we produce the minimum codes to pass that test, and finally refactor the codes.

Based on the book Test Driven Development: By Example by Kent Beck , we can break TDD down into 6 steps:

  1. Add a test: each new feature begins with writing a test. The developer can write a test by understanding the specifications and requirements through use cases and user stories. It makes the developer focus on the requirements before writing codes. This is a very important differentiating feature of TDD.
  2. Run all tests and see if the new test fails: this validates the test is working correctly, and the required feature does not already exist. The new test should also fail for the expected reasons. This step increases the developer's confidence that the unit is testing correct constraint and passes only in intended cases.
  3. Write some code: this step is to write some code that causes the test to pass. The only purpose is to pass the test even if the code written is not perfect or inelegant.
  4. Run tests: if all test cases pass then it means the new code meets the test requirements. If the test do not pass then adjust the code until it passes.
  5. Refactor the code: clean up the code, remove duplicates, improve readability and maintainability. This will be increasingly valuable later in the software lifecycle.
  6. Repeat: start a new test and make sure the steps are small with as few as 1 to 10 edits between each run. If new code does not quickly satisfy a new test or other tests fail then it's suggested to undo or revert rather than excessive debugging. This is where version control places a big role.
TDD Image

TDD Benefits and Limitations

Benefits:

  • By focusing on the test cases first, the developer must imagine how the functionality or feature is used by the end-users rather than preconceptions
  • This drives the design of the program as the developer have to be concerned with the interface before the implementation
  • TDD offers the ability to take small steps when necessary
  • TDD ensures all written code has been tested at least once
  • While TDD increases the total amount of code, it helps to limit the number of defects or bugs
  • Catching and preventing bugs early in the process avoid lengthy debugging later in the project
  • TDD can lead to more flexible codes because each cycle is written and tested independently and integrated together later

Limitations:

  • TDD does not perform sufficient testing where full functional tests are required to determine success or failure such as user interface, programs that work with databases or programs that depend on specific network configurations
  • Management support is essential, otherwise management may feel time spent writing tests is wasted
  • Unit tests are typically written by the developer who is also writing the code being tested. If the developer misinterprets the requirements then the code and unit tests are both incorrect therefore, the tests will pass and give a false sense of corectness
  • Writing and maintaining an excessive number of tests takes up time