←Previous page

10THINK LIKE A PROGRAMMER · TEST

Test small, test often.

Build one small step, run it, and check it with different values, even tricky ones.

IN REAL LIFE

Check as you go.

Good makers test a little at a time.

Tasting the soup

A cook tastes after adding salt, not only at the end.

A paper plane

You fold, throw it softly, fix a wing, and throw again.

Bike brakes

Before a ride down the hill, you test the brakes on flat ground.

LOOK FIRST

How programmers test.

Small steps

Write a little, run it, then add more. A new bug is in the new part.

Expect first

Say what should appear before you run. Then compare.

Tricky values

Try 0, an empty list, the biggest, the smallest, and the exact border.

LOOK FIRST

A test in one line.

== compares the result with what you expected.

def double(n):The function we want to test.
return n * 2It gives back the number times 2.
print(double(5) == 10)We expect 10. True means the test passed.
print(double(0) == 0)A tricky value: 0. It passes too.

RUN IT

Test the ride rule.

Guess first. Then run real Python and check.

1

Guess the result

def can_ride(age): return age >= 10 # Tests: True means passed print(can_ride(12) == True) print(can_ride(8) == False) print(can_ride(10) == True)

What will Python show?

tests.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Change >= to > and run again. Which test catches the change?

YOUR TURN

Test like a pro.

Try each tiny question. A wrong answer becomes something to review.

0Completed
12Not tried
0Review
01

Like a cook

A cook makes soup with five steps.

When is the best time to taste it?

02

Run it when?

You are writing a game with 30 lines.

When should you run it?

03

Not what you expected

print(3 + 4 * 2)

You expected 14. Python shows 11.

What do you do now?

04

Does the test pass?

def double(n): return n + 2 print(double(5) == 10)

What will Python show?

05

A tricky zero

def share(candy, kids): return candy // kids

Which test could break it?

06

An empty list

scores = [] best = scores[0]

What happens?

07

The exact border

if age > 10: print("Ride!")

Rule: kids 10 and older can ride.

Which test finds the bug?

08

A good set of tests

You test can_ride(age), where 10 and older can ride.

Which ages make the best tests?

09

Fix the border bug

Rule: 10 and older can ride. The test with 10 shows False. Fix the border.

Goal output
True
True
False
OUTPUT

10

Survive the zero test

The test with 0 friends crashes. If there are no friends, share should return 0.

Goal output
5
0
OUTPUT

11

Write the test

PyBot prints the result, but a test compares it. Change the last line so it shows True.

Goal output
True
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

Test small, test tricky.

Run after each small step. Say what you expect, then compare. Try 0, empty lists, and the exact border.