Tests as executable specs

A bridge formula that is never checked will fail on site. A test is a tiny program that calls your function and prints PASS or FAIL. Internships expect this habit before pytest. pandas pipelines and pygame games are unmaintainable without tests on the pure functions underneath.

PASSFAIL

PASS / FAIL card

Expected vs actual is a test.

You will be able to

  • Write expected vs actual checks
  • Keep tests next to the function

Arrange, act, assert

Set inputs, call the function, compare to a known answer. Print FAIL with both values when they differ. That trio is every unit test in disguise.

Test the boring cases: 0, 1, a typical value. Bugs hide at edges (late_fee(7) should be 0 extra days).

Tests protect the next library

When you later wrap the same function in pandas or pygame, the test still calls the plain function. That is why Advanced insists domain rules stay pure. pytest is this check() with nicer reports — the habit is identical.

A test that only prints the output is a demo, not a test. Without “want”, a wrong answer still looks busy.

Words that matter

Assertion
A check that got == want.

Common mistakes

Avoid: Only printing the function output.

Do this: Compare to an expected value.

Example program — Testing a unit converter

Break the function on purpose and watch FAIL.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — Test triple()

triple(n) returns n*3. Print PASS for triple(3) expecting 9.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. A test that only prints the function output…

Progress is stored in a browser cookie on this device.