Test-Driven Development – TDD

Test-Driven Development, TDD agile software development, Test-Driven Development cycle, writing code that passes tests

Test-Driven Development – TDD – is a software development practice where software requirements are converted to test cases before software is fully developed and software development is tracked by repeatedly testing the software against all test cases. TDD is opposed to traditional development flow, where software is developed first and test cases are created later.

Test-Driven Development

TDD involves writing automated unit tests before writing the code, coding just enough to pass the test at every step, focusing on passing the tests at each step until the final result is reached and then improving and simplifying the code.

TDD agile software development

Test-driven development is the practice of writing tests first with automated tests that prove the correct behavior of the code. The first step of TDD is writing a new step that fails, which means the test has gone red, then write the minimum code required for that test to pass, then for all the tests to pass, which means the tests have gone green, then refactor to improve the code and the tests and repeat the steps until the new feature is done. Tests should be small and incremental and commits should be made often, therefore if the new code fails, the need is just to revert instead of to debug.

Test-Driven Development cycle

  • add a test – write a test that passes if the specifications of the feature are met
  • run all tests. the new test should fail – showing that new code is needed for the feature and that the test is working correctly
  • write the simplest code that passes the new test – focus on coding just for the tested functionality with the smallest amount of code and not on code quality
  • all tests should pass now – if any tests are failing, the code should be reverted to the latest working version
  • refactor and run the tests after each refactor – improve the code for readability and maintainability, remove any hard-coded test data, run the test suite after each refactor to ensure that the tests pass against the refactored code
  • repeat the cycle – the TDD cycle is repeated for each new small piece of functionality being developed, running the previous tests as well as the added tests

Agile development practices