DevPrep

All topics › Software testing

TDD

Junior PRO

Essence

Test-Driven Development is a practice in which the test is written before the code and development proceeds in short Red → Green → Refactor cycles:

  1. Red: write a minimal test for the new behaviour; it fails (or does not even compile).
  2. Green: write the simplest code that makes the test pass. Elegance does not matter yet.
  3. Refactor: improve the structure of code and tests without changing behaviour; tests stay green.

A cycle takes minutes. Tests here are not an "after the fact" check but a design tool: they force you to decide first what the API should look like and what counts as a correct result.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store