DevPrep

All topics › Software testing

Unit tests

Junior PRO

Essence

A unit test verifies one unit of behaviour (a function, a type, a small module) in isolation from the outside world: no network, database, UI or timers. Dependencies are replaced with test doubles through protocols. The goal is to learn quickly and deterministically that a code change did not break expected behaviour. You test behaviour through the public contract, not every method in turn: tests written against the implementation break on every refactor.

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