Basic

A test is a checkable sentence

I used to click through the app and watch it, and that told me nothing the next Tuesday when the code changed. A test is a sentence about the program that the computer can re-check forever.

What you will see

Traceback (most recent call last): File "main.py", line 1, in <module> AssertionError

About 2 min of reading

Try It

Write one claim about a tiny function and see the computer confirm it.

1

Define a function that adds two numbers. This is the program under test.

def add(a, b): return a + b

Before you run it, say out loud what it will print. Then open the answer.

What it prints
>>>
2

Call it and look at the answer. You can see 5, but that is only you seeing it once.

add(2, 3)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
5
3

Now make the computer check it. The assert passes, so nothing prints and you get a clean prompt.

assert add(2, 3) == 5

Before you run it, say out loud what it will print. Then open the answer.

What it prints
>>>
4

Make a false claim. The assert raises and names the line, which is the report.

assert add(2, 3) == 6

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Traceback (most recent call last): File "main.py", line 1, in AssertionError
The AssertionError is the pass/fail signal. A bare assert is fine for one claim, but a real test file uses a runner so failures are collected and named instead of stopping at the first one.

Data Flow

You write a claim about the program, the computer runs it, and it either stays silent or raises a report of a mismatch.
1 You write a claim
↓
2 The program runs
↓
3 The claim is checked
↓
4 Silent pass or raised fail
Why this works
Why a test at all

You could read the code and reason about it. For a week that works. Then the code changes, or someone else changes it, and your reasoning is stale. A test moves the truth from your head into the machine. It is a claim you can run today, next week, and after a stranger edits the file.

When you have a choice

Test versus demo

A demo shows the program working once. A test states what must always be true and lets the computer confirm it on every run. The difference is the word always. A demo is a photograph, a test is a rule the photograph is checked against.

Examples

The sentence

assert add(2, 3) == 5

Read it as a promise: adding 2 and 3 must give 5. The computer runs it and either stays silent or raises. That silence is the whole meaning of a passing test.

It re-checks itself

assert add(2, 3) == 5
assert add(-1, 1) == 0

You write the sentence once and the computer runs it every time you run the file. It does not forget, it does not get bored, it does not need you to click. That is what makes it different from watching.

A failing test is a fact

assert add(2, 3) == 6

This one raises. That raise is not a mystery, it is a report: the program says 5 and you claimed 6. A red test is a machine-written note about a real mismatch.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

Next Lesson
Your first real test file
Your first real test file →