One broken rule at a time
level 1 · 15 min · equivalence partitioning (invalid partitions)
When several rules must hold at the same time, a common bug is one of them simply not being checked. And it goes unnoticed if every invalid password you test breaks several rules at once.
The way out is to isolate: start from a valid value and change only what is needed to break one rule. Each test then answers a single question.
Rule of thumb. Each invalid case breaks exactly one rule. And include a valid case right on the length limit.
Explore the rule
Type passwords and see which isValidPassword rules each one meets or breaks.
- 8+ characters: breaks
- has a digit: meets
- has a letter: meets
Exercise: write the tests
Your tests must pass on the correct code and fail on the 4 planted bugs.
isValidPassword(password) enforces three rules. A bug that drops one rule is invisible if every invalid password you test breaks several rules at once.
Write tests where each invalid password breaks exactly one rule, plus a valid password that sits right on the length boundary.
/**
* A password is valid when ALL of these hold:
* - it has at least 8 characters,
* - it contains at least one digit (0-9),
* - it contains at least one letter (a-z or A-Z).
* @param {string} password
* @returns {boolean}
*/
function isValidPassword(password)Coverage radar: ranges and limits your tests already touch
- A valid password of exactly 8 characters: not yet
- A 7-character password that breaks only the length: not yet
- A password that breaks only the digit rule: not yet
- A password that breaks only the letter rule: not yet
Passwords in your tests: "correcthorse42" (valid), "" (breaks 3)
Your tests
0 of 4 bugs detected
- Bug 1: hidden
- Bug 2: hidden
- Bug 3: hidden
- Bug 4: hidden