BOUNDARY VALUES

Bugs live on the edge of a rule, so put a test on the edge

L112 MINTESTING
PREDICT

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.

▶ SIMULATION
CODE UNDER TEST
price(): qty > 10 gets the 900 rate (the spec says 10 or more)
quantities it prices wrongly: 10
THE BUILD
tests run: 0
running
✓ the build is red whenever the code returns a wrong price✓ the build is green when the code is right
SCHEDULE
01[CODE]price(): qty > 10 gets the 900 rate (the spec says 10 or more)
02[SUITE]test: price(5) equals the spec
03[SUITE]test: price(30) equals the spec
04[SUITE]test: price(80) equals the spec
05[RUNNER]runner: report the build
06[RELEASE]release: the build is green, so ship it
Start of middle-of-range tests scenario. 6 steps.
// 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 band

Watch the invariant strip. In the first scenario three tests pass and the build is green, yet the code is wrong at one quantity.

TUNE IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

A rule changes at an edge, and a one-character mistake only shows there. Split the inputs into equivalence classes, test one value from inside each, then put a test on both sides of every edge: the last value out and the first value in.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: PROPERTY-BASED TESTING →