Designing Boundary Tests
Test each limit itself, one step inside it, and one step outside it, plus the empty case and a typical interior value. Boundaries deserve their own tests because comparisons fail by one exactly there.
A boundary test exercises the values where a program's behavior changes: the first accepted value, the last accepted value, and the values immediately outside. Most boundary defects are off-by-one errors or disagreements about whether an endpoint is inclusive, so each edge deserves an explicit test with an exact expected result.
The short answer
For every range, index, count, or capacity, test three points at each end: the boundary itself, one step inside, and one step outside. Add the empty case and a typical interior value. Write the expected result for each; a test that only checks that nothing crashes cannot detect a wrong endpoint.
| Value | Example for clamp(x, 0, 100) |
What it exercises |
|---|---|---|
| below minimum | -1 | rejection or lower clamp |
| minimum | 0 | inclusive lower edge |
| just above minimum | 1 | normal path |
| interior | 50 | baseline |
| just below maximum | 99 | normal path |
| maximum | 100 | inclusive upper edge |
| above maximum | 101 | rejection or upper clamp |
A minimal example
import pytest
def clamp(value, low, high):
if low > high:
raise ValueError("empty range")
return max(low, min(value, high))
@pytest.mark.parametrize("value,expected", [
(-1, 0), (0, 0), (1, 1),
(99, 99), (100, 100), (101, 100),
])
def test_clamp_at_boundaries(value, expected):
assert clamp(value, 0, 100) == expected
def test_clamp_rejects_inverted_range():
with pytest.raises(ValueError):
clamp(5, 10, 0)
A property test complements the table by covering everything between the points:
from hypothesis import given, strategies as st
@given(st.integers(), st.integers(min_value=0, max_value=100))
def test_clamp_stays_within_bounds(value, high):
assert 0 <= clamp(value, 0, high) <= high
Why this happens
Comparisons encode inclusivity: < versus <=, a loop to n versus n + 1, a half-open versus a closed interval. Each choice creates a transition point where implementations disagree. Failures cluster at those points rather than distributing evenly, which is why a handful of edge cases finds a disproportionate share of defects. Binary search is the standard illustration: the same algorithm returns different indices depending on the midpoint comparison, and the left-boundary variant works through the difference.
What changes the answer
Inclusive and exclusive conventions change which value is the edge. An empty range may be valid (low == high) or an error (low > high). The type changes the special values: integers have minimum and maximum limits and overflow; floats have signed zero, infinities, and NaN; strings have empty, whitespace-only, and multi-byte characters; dates have month ends and daylight-saving transitions. Collection semantics matter too: slicing past the end may be legal while indexing past the end is not. Business rules, such as an age of exactly 18, are boundaries even though no arithmetic is involved.
Edge cases
- Pagination: the last partial page and the page after it.
- Capacity: an insert when the buffer is exactly full.
- Time: the instant a deadline expires, and leap days.
- Encoding: a string whose length differs in bytes and code points.
- Rounding: values exactly halfway between representable results.
Practical checks
Put boundary values in a parametrized table so the edges are visible in the test report. Assert exact outputs, not just the absence of exceptions. Enable branch coverage to confirm that both sides of each comparison run. Add a property test for the invariant, then keep the explicit edge cases as regression tests. When a bug is fixed, add the specific value that failed to the table so the same edge cannot regress.
Sources
Revision history
- Published
- Updated
- — Initial publication.