The TDD Pill—What Is It?

The ingredients of the TDD pill are as follows and should be prepared following the given order exactly:

  1. Choose a small task.

  2. Write a test for that task.

  3. Run all the tests to verify that the new test fails.

  4. Write minimal production code to complete the task.

  5. Run all tests (including the new one) to verify that they pass.

  6. Refactor the code as necessary.

  7. Repeat from step 1.

The active ingredient in the TDD pill is the authoring of test cases before production code. Authoring test cases before code requires the patient to consider the design of the solution, how information will flow, the possible outputs of the code, and exceptional scenarios that might occur. Running the newly written test case before writing production code helps to verify that the test is written correctly (a passing test case at this point is not testing the intended effects) and that the system compiles. The TDD pill also involves writing just enough production code to pass the test case, which encourages an uncluttered, modular design. Furthermore, TDD users create a growing library of automated test cases that can be executed at any time to verify the correctness of the existing system whenever changes are made.

Like many drugs, the TDD pill has some official variants, including ATDD (Acceptance-Test-Driven Development), BDD (Behavior-Driven Development) and STDD (Story-Test-Driven Development). ATDD replaces the “small task” step with “functional level business logic tasks,” whereas BDD uses ...

Get Making Software now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.