Beginner 11 min

Test boundaries and edge cases

Choose examples that prove the limits of a rule and check for common off-by-one errors.

State the rule before picking cases

Imagine a voucher is valid for totals of at least 20. Write the rule in your own words and identify its boundary. A normal example tells you whether the common path works. An example at the threshold tells you whether your comparison includes it.

How it works

Write a small set of examples around each rule. If a discount starts at ten items, compare an order of nine, an order of ten, and an order of eleven. These examples expose whether the code treats the cutoff as inclusive or exclusive.

Check the value on both sides

For whole-number totals, choose 19, 20, and 21. This set catches an accidental > instead of >=. Decide separately how unsupported types and invalid values should behave. Record each expected result before implementing the logic.

Code example

def qualifies(total):
    return total >= 20

assert not qualifies(19)
assert qualifies(20)
assert qualifies(21)

Check your understanding

A locker room allows a group size from 1 through 8 inclusive. Decide which boundary values are accepted.

See the example and explanation
Example 1
Given 0, 1, 8, 9
Answer Reject, accept, accept, reject

One and eight are accepted because both boundaries are inclusive. Zero falls below the lower limit, and nine exceeds the upper limit.

Open practice problem
Next lessonHow an HTTP request works