Both sides of a rule
level 1 · 10 min · equivalence partitioning
A test exists to fail when the code is wrong. A test that passes with any code protects nothing, however green it looks.
Every “if… then…” rule has two sides: when it holds and when it does not. Testing only the happy side leaves half the bugs free.
Rule of thumb. For each rule, write at least one test where the answer is yes and one where it is no. Then bring the two closer to the boundary.
Explore the rule
A signup form uses isAdult(age). Drag and see which side of the rule each age falls on.
Exercise: write the tests
Your tests must pass on the correct code and fail on the 3 planted bugs.
A sign-up form calls isAdult(age) to decide who may create an account. Write tests for it.
Your tests run twice: once against the correct code, where they must all pass, and once against each of three broken versions, where at least one test must fail. A test that passes no matter what the code does protects nothing.
The starter test passes, but it does not catch any of the bugs. Run it to see.
/**
* Returns true when the person is an adult (18 or older), false otherwise.
* @param {number} age Whole years.
* @returns {boolean}
*/
function isAdult(age)Coverage radar: ranges and limits your tests already touch
Values in your tests: 30. Missing: the “minor” range, the limit 18, the value just below 18.
Your tests
0 of 3 bugs detected
- Bug 1: hidden
- Bug 2: hidden
- Bug 3: hidden