Course outline · 0% complete

0/29 lessons0%

Course overview →

Capstone Part 1: Write the Tests

lesson 10-1 · ~13 min · 28/29

The brief

A teammate left the company and left behind wallet, a small module the payments page depends on. Users report odd behavior. Your job across these two lessons: pin the module down with tests, then find and fix every bug.

The agreed spec:

FunctionContract
new_wallet()returns a wallet with balance 0 and empty history
deposit(w, amount)amount must be strictly positive, zero or less raises ValueError
withdraw(w, amount)strictly positive, may equal the balance exactly, only amounts greater than the balance raise ValueError
both operationsappend ("deposit", amount) or ("withdraw", amount) to w["history"]

Today you only write tests. Lesson 4-1 taught why seeing red first matters: each failing test is a discovered bug, documented and reproducible before you change a line.

Code exercise · python

Two tests are written and green. Add the three missing spec tests: test_deposit_zero_rejected (expect ValueError, lesson 2-2's try/assert False/except pattern), test_withdraw_exact_balance_allowed (deposit 50, withdraw 50, balance 0), and test_history_records_withdrawals (last history entry is ("withdraw", 20) after depositing 50 and withdrawing 20). Register them in the runner list in that order, between and after the existing ones as shown by the expected output. Do NOT fix the module yet, your reward today is the red report below.

Freeze the red

You now hold a red report with three named failures, which is a complete bug inventory:

  1. deposit(w, 0) is accepted, spec says reject
  2. withdrawing the exact balance raises ValueError, spec says allow
  3. withdrawals are recorded in history as "deposit"

Notice what the tests bought you before any fixing: the bugs are reproducible on demand (lesson 7-2), named (lesson 2-2), and the fix will be provable by a red-to-green flip (lesson 4-3). Carry your five tests into the next lesson unchanged.

Quiz

Two of the three failing tests point at boundary bugs. Which lesson 3-2 signature do the deposit-zero and withdraw-exact failures share?