Boundary values: where bugs cluster
level 1 · 15 min · boundary value analysis
Every rule with a number draws lines: “from 100 up, shipping is free”, “below 0 is an error”. Bugs cluster exactly on those lines, because that is where a > turns into >= without anyone noticing.
The technique has a name, boundary value analysis, and is one of the cheapest and most effective there is: a few well-chosen tests catch most comparison errors.
Rule of thumb. For each line, test the last value on one side and the first on the other. If the line is at 100, test 99 and 100. Do not forget zero.
Explore the rule
The store charges shipping with shippingFee(total). Drag and see where the answer changes.
Exercise: write the tests
Your tests must pass on the correct code and fail on the 4 planted bugs.
A shop charges shipping with shippingFee(total). Read the rules in the contract and write tests that catch the four bugs.
This time there are two boundaries and an error. To check that a function throws, wrap the call: expect(() => shippingFee(-1)).to.throw("Invalid total").
/**
* Shipping fee for an order.
* - Orders of 100 or more ship free (fee 0).
* - Orders below 100 pay a fee of 10.
* - An empty order (total 0) also pays 10.
* - A negative total is a mistake: throws Error("Invalid total").
* @param {number} total
* @returns {number}
*/
function shippingFee(total)Coverage radar: ranges and limits your tests already touch
Values in your tests: 250. Missing: the “error” range, the “pays 10” range, the limit 0, the limit 100, the value just below 0, the value just below 100.
Your tests
0 of 4 bugs detected
- Bug 1: hidden
- Bug 2: hidden
- Bug 3: hidden
- Bug 4: hidden