Skip to content

Testing & QA

What a Test Is (and Why We Write Them)

The idea, in plain English

A test is a second, tiny program whose only job is to check that your real program does what you promised. Think of a smoke detector. You don't wait for a fire to find out it works — you press the button and listen for the beep. A test presses the button on your code: it feeds in a known input, checks the output is what you expected, and beeps 'PASS' or 'FAIL'. If someone later breaks that code by accident, the test starts failing, and you find out in seconds instead of from an angry user.

How it works

  1. 1Pick one thing your code should do, like 'adding 2 and 3 gives 5'.
  2. 2Run the real code with that known input and capture what it actually returned.
  3. 3Compare the actual result to the result you expected ahead of time.
  4. 4If they match, the test passes. If they don't, it fails and points at exactly what went wrong.

When you'd use it

Write tests for any code whose correctness matters and that you'll change again later — which is almost all real code. They pay off most on logic that's easy to break by accident: money math, dates, permissions, and anything many other parts depend on.

Common beginner mistakes

  • Thinking 'it ran without crashing' means it's correct. A function can run fine and still return the wrong answer — a test checks the answer, not just that nothing exploded.
  • Only testing the cases you know work. The valuable tests cover the tricky edges: zero, empty input, negatives, and the 'that could never happen' case that happens in week two.

Try it — edit and run

Click the code to edit · press ⌘/Ctrl+↵ to run

Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.

Expected output — hit Run to try it
PASS: 2 + 3 (expected 5, got 5)
PASS: 10 + 5 (expected 15, got 15)
FAIL: 0 + 0 (expected 1, got 0)

Not sure this is the right topic? See the learning paths → or where this leads →