Course outline · 0% complete

0/29 lessons0%

Course overview →

Finding the Cases You Forgot

lesson 3-1 · ~10 min · 7/29

Quiz

Warm-up from lesson 2-1: in the arrange, act, assert pattern, how many calls to the function under test should the act phase make?

The edge-case checklist

Most bugs do not live in the typical case, they live at the edges: inputs the author never pictured. You can hunt them systematically instead of hoping. For any function, walk this checklist:

  • zero / empty: 0, "", [], {}
  • one: a single element or character
  • many: a normal, busy input
  • extremes: the largest and smallest sensible values, negatives
  • duplicates and ties: two items that compete for the same answer
  • weird but legal: extra spaces, mixed case, unicode

Each checklist line becomes one focused test, named the way lesson 2-2 taught. The habit takes two minutes per function and catches a shocking share of real-world crashes.

Code exercise · python

Run this. longest_word gets one test per checklist line that applies: many, one, empty, and a tie. Note the tie test pins down which of two equal-length words wins.

Quiz

You are testing find_cheapest(prices). Which input from the checklist is MOST likely to crash a naive implementation?

Code exercise · python

Your turn. The team decided safe_average([]) should return 0.0, and the test for it exists, but the code crashes with ZeroDivisionError. Run it to see the crash, then fix safe_average so both tests pass.

Code exercise · python

Your turn to walk the checklist unaided. For total_length, write four tests and run them with the loop: test_many_words ("hi", "there" totals 7), test_one_word ("solo" totals 4), test_empty_list (0), and test_space_padding_counts (the weird-but-legal case: " a " has length 3, spaces count).