Choosing Testable Program Inputs
An example becomes more useful when changing one input can distinguish between two plausible implementations.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 2 min
In this article4 chapters
Separate competing formulas
Suppose a program charges two credits per copy plus five credits per job. With one copy, charging seven per copy happens to agree. With three copies, the correct result is eleven and the other formula gives twenty-one. A sample with a stated output can reveal this difference without requiring the reader to inspect code.
Choose a case for each boundary
Try a zero-copy order if it is allowed. Should the job charge still apply, or is a zero quantity rejected? An example after the limit can be equally informative when quantities are capped. Document the decision in a function contract before making either behavior an expected test result.[1][2]
Start from wrong implementations that seem plausible
For a fixed service fee of five and a per-item charge of two, an implementation using seven per item gives the same answer for one item. Input three distinguishes them: eleven versus twenty-one. Another candidate could apply the fixed fee only after the first input line. If zero items form a valid request, determine whether its result is zero, five, or a distinct rejection under the stated policy. Testing zero is useful only after the expected behavior is known.
The reasoning applies to many small bugs. For an ordered filter, choose input whose retained records are neither already sorted by label nor equal to each other. For a converter from seconds to milliseconds, choose a fractional number if the interface supports it. The normal and boundary cases should cover the actual independent decisions.
Write independent expected results
For a count algorithm, compute an expected answer by listing the accepted items. Avoid deriving the test expectation by repeating the production expression. Two copies of a mistaken expression can agree. A useful table has input, expected output, and a short explanation of what each row distinguishes. That explanation helps a reviewer recognize when a later edit removes an important case by accident.
Practice with a rate of six percent applied to amounts of ten and fifty. The expected amounts are 0.6 and 3 under a fractional-rate convention. A first test of amount zero would fail to reveal an extra division by one hundred. Ratios and fractional rates specify which meanings must survive the conversion.