Test-driven development

Testing Junior Swift 5.0+ iOS 17+ Reviewed By Alok Singh

Order the steps

Put one TDD cycle in order.

  • Run it and watch it fail
  • Refactor while everything stays green
  • Run all the tests
  • Write the minimum code to make it pass
  • Write a test for the next small behaviour
Hint 1 of 1

Red, green, refactor.

Choose an answer, then open Show answer below to check it.

Show answer

Answer: E A D C B

  1. Write a test for the next small behaviour Start with the behaviour you want.
  2. Run it and watch it fail A failing test proves it can catch the problem.
  3. Write the minimum code to make it pass Just enough code keeps the design honest.
  4. Run all the tests Make sure nothing else broke.
  5. Refactor while everything stays green Clean up with the tests as a safety net.

30-second answer

Test-driven development is a short loop: write a failing test for the next small behaviour, write just enough code to pass it, then refactor while the tests stay green. It works best for logic with clear inputs and outputs, such as parsers, pricing rules and view models, and less well for exploratory UI work.

Practise saying it · 30 s. No recording, just a timer

Explain TDD and when you use it.

Say your answer out loud, then check which of these points you covered:

  • Red, green, refactor
  • Small steps
  • Where it fits

Deep dive

The failing step proves the test can fail.

Small steps keep the design simple.

Bug fixes start with a test that reproduces the bug.

TDD tests describe behaviour, which documents the code.

Common mistakes

  • Writing tests after the fact and calling it TDD.
  • Skipping the refactor step.

Senior take & follow-ups

Seniors use TDD where it pays off and always start bug fixes with a failing test.

Follow-up questions

Think of your answer first, then open the hint.

  1. Why watch the test fail first?

    Show hint

    To prove it tests something.

  2. Where does TDD fit badly?

    Show hint

    Exploratory UI and prototypes.

Suggest a fix if something here is wrong or out of date.