BOUNDARY VALUES
Bugs live on the edge of a rule, so put a test on the edge
Bulk pricing: 1 to 9 units cost 1000 each, 10 to 49 cost 900 each. The code says if (qty > 10) where the spec says 10 or more. The suite tests 5, 30 and 80, and passes. You may add one test. Which catches the bug?
A function with tiered rules has edges: the quantity where the price changes, the age where a rule starts, the size where a limit bites. The code compares with > or >=, and one character is the whole bug. Between the edges the correct code and the buggy code give the same answers, so tests in the middle of a band pass for both.
Group the inputs into equivalence classes: sets the code should treat alike. Test each class once. Then test the edges between them, on both sides: the last value out and the first value in.
// spec: 10 or more units get the 900 rate
if (qty > 10) return qty * 900; // off by one: 10 is charged 1000 each
// tests
expect(price(5)).toBe(5000);
expect(price(30)).toBe(27000);
expect(price(80)).toBe(64000); // every one in the middle of a bandif (qty >= 10) return qty * 900;
// one case on each side of every edge, plus the limits
[0, 1, 9, 10, 49, 50, 200, 201].forEach(q =>
expect(price(q)).toBe(spec(q))); Watch the invariant strip. In the first scenario three tests pass and the build is green, yet the code is wrong at one quantity.
The suite has three tests, and the starting values (5, 30 and 80) are the ones the team chose. They all pass. Move the three quantities until one fails. The bug is in a single quantity.
GOAL: at least one test fails
Write cases(), which returns an array of [quantity, expectedTotal] pairs. Spec: quantity 1 to 9 costs 1000 each, 10 to 49 costs 900 each, 50 to 200 costs 800 each, and anything else (0, negative, above 200) is refused: expected null. Work out the equivalence classes, then put cases on each edge. The harness runs your cases against the correct function (all must pass) and against eight versions that each have one off-by-one; every one of them must fail at least one of your cases.
Your cases are only as good as the bugs they catch. The harness checks that by breaking the function eight ways and counting the red builds. That is the idea behind mutation testing, and for a function this small you can do it by hand. For inputs you cannot list, see property-based testing.
Pricing tiers, shipping bands, tax thresholds, age limits, rate limits, pagination (page size 0, 1, exactly full, one over), string length limits, and integer overflow. Anything with a number in the spec has an edge. Tax and fee tables are the classic place for it, because the boundary belongs to one band and everyone remembers it differently.
“How would you test a field that accepts ages 18 to 65?” Say 17, 18, 65 and 66, plus one middle value and the type edges (empty, negative, text). Then explain why: equivalence classes for the middle, boundaries for the off-by-one. A strong answer adds how you would check the tests: change each comparison and see which ones fail.
USED IN
Chapters that build on this lesson.
- A first unit testSoftware Testing and Quality
- Choosing cases: boundaries, equivalence classes and the unhappy pathSoftware Testing and Quality
- Project: put a legacy function under test and find its bugSoftware Testing and Quality
- Property-based testing: say what is always true, let the machine huntSoftware Testing and Quality