Lecture Notes — Lecture 08: Testing Before Trusting

Companion to the lecture plan and the worked examples. Read the Definitions before class, the Explanation after it; the practice questions and the lecture quiz close the loop.

Definitions

Explanation

The test table is the specification. A requirement like 'valid age is 0-120' implies at least five tests (below, both edges, above, typical) before any code exists. Writing the table first is cheaper than debugging later.

Three case classes. Normal (typical use), boundary (exactly at edges), adversarial (designed to break naive code: empty, negative, huge, wrong type). Boundary finds the most defects per case; adversarial finds the embarrassing ones.

Black-box vs white-box. Black-box: from the spec, so it also catches missing features. White-box: from the code, so it reaches awkward branches. They are complements, not rivals - cs-031 makes students feel the difference.

Coverage, honestly. Aim for 'every branch is reached by at least one case', not percentage badges. 100% line coverage with zero boundary cases is a common and worthless achievement.

Testing randomness. A random function cannot be pinned by one outcome. Either fix the seed (deterministic, reproducible) or assert distribution invariants over many trials (mean in range, values in range, repeats allowed). cs-032 builds the habit.

Cost arithmetic. A test costs minutes; a defect found by a user costs days. Three boundary tests before the five-minute attempt is the best time investment in this course.

Examples

The lecture's worked examples live in examples.md; the case studies this lecture builds on are worked in class from the projector. The case sequence with difficulties:

Common misconceptions

Summary and key takeaways

  1. Write the test table before the code; it is the spec made executable.
  2. Normal, boundary, adversarial - three classes, always.
  3. Black-box and white-box complement each other.
  4. Randomness: fix the seed or assert distributions - never one outcome.

Practice questions

Where this leads

Next lecture: Aggregation Patterns. The quiz below checks this lecture's essentials before we build on them.