How do you pick boundary values to catch off-by-one bugs?
Question
A checkout function shipping_fee(total_cents) prices integer cart totals in cents:
total_cents <= 0: raiseValueError1to4999($0.01to$49.99): return5005000to9999($50.00to$99.99): return20010000($100.00) and above: return0
Tests checked 2500, 7500, and 15000 and passed, yet $50.00 carts still paid $5.00 in production.
- Why did those tests miss the bug?
- List the minimal 2-value boundary inputs, and say what 3-value BVA adds.
- Fix
shipping_feeand write a compact boundary test table.
def shipping_fee(total_cents: int) -> int: if total_cents <= 0: raise ValueError("total must be positive") if total_cents <= 5000: return 500 if total_cents < 10000: return 200 return 0
Show hints (3)
- "CHECK PARTITION EDGES"
2500,7500, and15000sit in the middle of each price band. Where does<vs<=actually change the result? - "PAIR EVERY EDGE"
The four partitions meet at three edges:
0 | 1,4999 | 5000, and9999 | 10000. Test both sides of each edge. - "ADD INNER NEIGHBORS"
2-value BVA uses 6 inputs. 3-value BVA adds each edge's inner neighbor (
-1,2,4998,5001,9998,10001) to catch==mistakes.
Show checked explanation
Midpoints (2500, 7500, 15000) never touch the 4999 | 5000 edge where <= 5000 fails. Use 2-value boundary value analysis across all three edges (0, 1, 4999, 5000, 9999, 10000, 6 cases) to catch every < vs <= off-by-one bug, and add 3-value neighbors when == mistakes are a risk.
def shipping_fee(total_cents: int) -> int: if total_cents <= 0: raise ValueError("total must be positive") if total_cents < 5000: return 500 if total_cents < 10000: return 200 return 0 VALID = [(1, 500), (4999, 500), (5000, 200), (9999, 200), (10000, 0)] for cents, expected in VALID: assert shipping_fee(cents) == expected, cents try: shipping_fee(0) raise AssertionError("0 must fail") except ValueError: pass print("6 boundary cases passed")
6 boundary cases passed
Executed the solution on CPython 3.12.10, proved midpoint tests pass on the buggy <= 5000 starter, and mutated every boundary comparison against 2-value and 3-value BVA sets. Checked by an editor other than the author.
Walkthrough
Equivalence partitioning splits inputs into groups that the spec treats the same way. Here there are four integer partitions: invalid (<= 0), standard (1..4999), reduced (5000..9999), and free (>= 10000). Picking one midpoint per valid band (2500, 7500, 15000) covers all three valid partitions yet skips every comparison edge.
Off-by-one bugs live at partition edges, where a developer writes <= instead of < or shifts a constant by one cent. 2-value boundary value analysis (BVA) tests each boundary value and its immediate neighbor across the edge. For integer cents (step size 1), the three edges give 6 cases:
| Edge | Invalid / lower side | Upper side | Off-by-one bug caught |
|---|---|---|---|
0 | 1 | 0 -> ValueError | 1 -> 500 | < 0 instead of <= 0 |
4999 | 5000 | 4999 -> 500 | 5000 -> 200 | <= 5000 or < 4999 |
9999 | 10000 | 9999 -> 200 | 10000 -> 0 | <= 10000 or < 9999 |
On the starter code (if total_cents <= 5000), shipping_fee(5000) returns 500 instead of 200, so the 6-case table fails immediately. Replacing <= 5000 with < 5000 makes every boundary pass.
What 3-value BVA adds. Under ISTQB CTFL v4.0, 3-value BVA tests b - 1, b, and b + 1 for every boundary b. Merging overlapping neighbors across the six boundary values (0, 1, 4999, 5000, 9999, 10000) yields 12 unique inputs: -1, 0, 1, 2, 4998, 4999, 5000, 5001, 9998, 9999, 10000, 10001. Why bother? If someone mistakenly wrote if total_cents == 10000: return 0, 2-value BVA passes on 9999 and 10000, whereas 3-value BVA catches the bug at 10001.
Keep the test table data-driven (for example @pytest.mark.parametrize) so each boundary reads like the spec and fails with the exact input that broke.
Common mistakes
- Testing only partition midpoints (
2500,7500,15000). That confirms the happy bands exist while missing every<vs<=bug. - Using floating-point dollars (
49.99,50.00) instead of integer cents. Binary float rounding can hide or invent boundary errors. - Testing only
4999and5001, skipping5000itself. The exact threshold is the single value where< 5000and<= 5000diverge. - Skipping the invalid partition (
0and-1). Missing that edge lets free or negative carts slip through withoutValueError.
Follow-up questions
- "What changes if the cart total is a decimal dollar amount with two decimal places?" The step size shrinks from
1cent to0.01dollars, so the50.00edge becomes49.99and50.00(with50.01for 3-value BVA). You also need a case for three decimal places like49.995to check rounding rules. - "Does 100% 2-value BVA replace domain edge cases like huge numbers or non-integers?" No. BVA covers the spec's numerical edges. You still add targeted checks for
None, booleans (True == 1in Python), strings, or very large integers if the caller is untrusted. - "When do you choose 3-value BVA over 2-value BVA?" Use 2-value BVA by default for fast unit suites. Step up to 3-value BVA on high-risk billing, tax, or safety rules where a stray
==or!=check could pass a 2-value table.
What a strong answer covers
Maps all four partitions, derives the exact 6-case 2-value boundary table, explains why midpoints miss <= 5000, and names the == defect that 3-value BVA (12 inputs) catches.
Open in interactive practice deck →