A Good Test Distinguishes Plausible Wrong Results

Choose an input for which a realistic implementation mistake gives a different result from the promised behavior.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Think of two formulas
  2. Check order with labels
  3. Construct a case that fails a plausible incorrect implementation
  4. Keep each failure explainable

Think of two formulas

A mean routine should divide by the number of items. Dividing by the largest item gives the same result for values 1, 2, and 3 because the largest is three. Try values 2, 2, and 6 to distinguish those denominators. Testable inputs follow this same principle.

Check order with labels

Values that are equal can hide an ordering bug unless each is labeled. A stable tie test should carry distinct IDs with equal sort keys. See stable ordering.[1]

Construct a case that fails a plausible incorrect implementation

A function returning first maximum index can confuse value and position. On [2,7,7,1], the value is seven and first index is one. Returning two would also disclose that the code overwrote a previous match on equality. See finding both extremes.

Desired answer: index 1
Wrong value/index swap: 7
Wrong tie rule: index 2
Adding a duplicate turns the tie into an observable condition

Keep each failure explainable

A compact test can detect several mistakes when expected outcomes remain distinct. Add a singleton or empty case separately if it exercises a different contract. Validate that the test would fail when the defect it targets is present.

References

  1. Python Software Foundation, unittest: Unit Testing Framework ↩