Tests as an executable spec

PASS/FAIL next to the wall-notice numbers. unittest, pytest and doctest are laptop runners. A green bar only proves the cases you wrote.

PASSFAIL

PASS / FAIL card

Expected vs actual is a test.

You will be able to

  • Write PASS/FAIL next to a function as an executable spec
  • Put wall-notice numbers in the test, not only in a comment
  • Name unittest, pytest and doctest as laptop runners — a green bar only proves the cases you wrote

A formula that is never checked will fail on the first inspection

PASS/FAIL printed next to late_fee is the spec. Internship code without this habit gets rewritten — not because the author was unkind, but because nobody can prove the 4-day fee is still 25 after a “quick fix”. On a laptop you will use pytest, unittest, or doctest (examples inside the docstring). Here, a helper that prints PASS or FAIL is the same idea: got vs want, in public.

Put the wall notice into the test: late_fee(0) is 0, late_fee(4) is 25. When the notice changes, the test fails on purpose. That failure is the point. A test that only prints “ok” with no numbers is theatre.

A green bar is not omniscience

PASS means the cases you wrote still hold. It does not mean the function is right for 8 days if you never asserted 8 days. Add the bands you care about. unittest lives in the stdlib. Many teams add pytest for shorter assert lines. doctest runs the >>> snippets in a docstring — useful for a tiny function, easy to rot if you never run it. The tool is secondary. The executable examples are the product.

Do not delete a failing test because “we know it works”. Fix the function or update the spec on purpose.

Laptop: unittest sketch and a doctest line

def late_fee(days):
    """INR hostel late fee.

    >>> late_fee(0)
    0
    >>> late_fee(4)
    25
    """
    if days <= 0:
        return 0
    return 25

import unittest
class TestLate(unittest.TestCase):
    def test_mid(self):
        self.assertEqual(late_fee(4), 25)

Words that matter

Assertion
got vs want — a line that must be true.
pytest / unittest
Laptop runners that collect tests and report failures.
doctest
Laptop: runs >>> examples inside a docstring.
Spec
Examples the wall notice and the function must both satisfy.

Common mistakes

Avoid: Deleting a failing test because “we know it works”.

Do this: Fix the function or update the spec on purpose.

Run it step by step

Each box is a real program. Press Run, change a number, Run again — the output must follow your code.

1. Step 1 — one check

Change late_fee to return 99 and watch FAIL appear.

Python sandboxlesson://workspace
console

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

build

Press Build to compile.

2. Step 2 — two examples from the notice

On-time and mid band. Both must hold.

Python sandboxlesson://workspace
console

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

build

Press Build to compile.

On a full Python install — pytest

Collects test_*.py, shows a traceback on mismatch. Install into a venv, not the college global Python. unittest is stdlib; doctest runs docstring examples.

pip install pytest

The same late_fee examples, collected automatically.

Real library code (not run in this browser sandbox)

def late_fee(days):
    if days <= 0:
        return 0
    if days <= 7:
        return 25
    return 50

def test_on_time():
    assert late_fee(0) == 0

def test_mid_band():
    assert late_fee(4) == 25

Example program — A tiny spec file

This is test_fees.py energy — examples in code, including the 9-day band.

Python sandboxlesson://workspace
console

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

build

Press Build to compile.

Your turn — Assert 88

pf(100) should be 88. Print PASS if it is, FAIL otherwise. Make it PASS.

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 compares…
2. A passing test means…

Progress is stored in a browser cookie on this device.