The idea: a few inputs stand in for all of them

A function that takes one integer has billions of possible inputs. Nobody can test them all. A tester must pick a handful, and pick them so that a bug is likely to show. Equivalence partitioning and boundary value analysis are the two basic ways to pick. Both are black-box techniques: they read only the specification, never the code.

A number line from -2 to 13 split into three equivalence classes: qty 0 or less is invalid, 1 to 10 is valid, 11 or more is invalid. One test input is picked from each class: -2, 5 and 13, with the expected results reject, accept, reject
The spec splits the inputs into three classes; one test from each stands in for the whole class.

The page tests canOrder(qty). The spec says: accept an order when 1 ≤ qty ≤ 10, otherwise reject it. The canvas shows every integer from −2 to 13, the code, and a table of tests. Each test flies to its input on the number line and gets a ✓ or a ✗.

Equivalence classes

An equivalence class is a set of inputs that the spec treats in the same way. If the program handles one member of the class correctly, it very likely handles the others too. Here there are three:

ClassInputsExpected result
invalid, too smallqty ≤ 0reject
valid1 ≤ qty ≤ 10accept
invalid, too largeqty ≥ 11reject

The classes must cover every input and must not overlap. Invalid classes count: much real damage comes from input the programmer did not expect.

One test per class

Equivalence partitioning takes one input from each class: −2, 5 and 13. Three tests replace billions. Demo 1: one test per class, correct code.

This is enough to find a bug that breaks a whole class. If the programmer forgot the upper limit and wrote qty >= 1, then every input above 10 is accepted. The test with 13 fails. Demo 2: missing upper limit, a class test finds it.

Boundary values

Many bugs are wrong for only one input, and that input sits at the edge of a class. Typing < for <=, or 0 for 1, is the classic off-by-one error. With qty < 10 the code is wrong for 10 and right for every other integer. A test with 5 passes. Demo 3: off-by-one, class tests miss it.

Boundary value analysis puts tests on the edges. For a valid range from min to max it uses six values:

Testmin − 1minmin + 1max − 1maxmax + 1
qty01291011
expectedrejectacceptacceptacceptacceptreject

The test with 10 expects accept and gets reject. The bug is found. Demo 4: off-by-one, the boundary test catches it. The same six tests catch qty >= 0, which wrongly accepts 0. Demo 5: accepts 0, caught at min − 1.

The number line with the six boundary tests 0, 1, 2 and 9, 10, 11 marked. The code uses less-than 10 instead of less-or-equal 10, so it is wrong only for 10. The class test at 5 passes; the boundary test at 10 fails and finds the bug
An off-by-one bug is wrong for a single input; only a test on that boundary can fail.

What each suite finds

CodeWrong forOne per class (−2, 5, 13)Boundary values (0, 1, 2, 9, 10, 11)
qty >= 111 and above✓ found at 13✓ found at 11
… qty < 1010✗ missed✓ found at 10
qty >= 0 …0✗ missed✓ found at 0
… && qty != 77✗ missed✗ missed

The last row is the limit of both techniques. Nothing in the spec says 7 is special, so no test picks it. Demo 6: bug at 7 only, both techniques miss it. To find such a bug you must look inside the code. That is white-box testing: see Control-Flow Coverage.

In practice the two techniques are used together: first find the classes, then test their edges. The six boundary values already contain a member of every class, so the mid-class tests add little here. They matter when the code treats the middle differently.

Two inputs: a grid

canBook(qty, days) accepts when 1 ≤ qty ≤ 10 and 1 ≤ days ≤ 30. Each input has three classes, so the pairs form a 3 × 3 grid of nine cells. Only the middle cell is valid. The code on the page forgot the check days <= 30, so it is wrong in one cell: valid qty, days too large.

Three tests can use every class once: (5, 15), (−2, −3) and (13, 40). All three pass. The value 40 appears only next to qty = 13, and the qty check rejects the pair first. One invalid value masks the other. Demo 7: two inputs, each class once, bug masked.

The fix is a rule: one invalid value per test. Keep the other input valid, so only one check can cause the reject. That takes five tests, and (5, 40) fails. Demo 8: two inputs, one invalid at a time.

Two 3 by 3 grids of qty classes against days classes. Left: three tests on the diagonal use every class once, none lands in the buggy cell, bug missed. Right: five tests in a plus shape, one invalid value at a time, the test (5, 40) lands in the buggy cell and fails
Three tests cover every class but leave the buggy cell empty; five tests with one invalid value each reach it.
SuiteTestsCells usedFinds the missing check?
each class once33 of 9✗ no, masked
one invalid at a time55 of 9✓ yes, at (5, 40)
every cell99 of 9✓ yes, at (5, 40)

Testing every cell grows fast: with n inputs of three classes each it needs 3n tests. One invalid value at a time needs only 2n + 1.

See also Test Doubles for how a unit test replaces the collaborators of the code under test, and Searching a Sorted List, where binary search is a classic place for off-by-one errors at the ends of a range.

See also Control-Flow Coverage for white-box testing: statement, branch and path coverage on a control-flow graph.

See also Condition Coverage and MC/DC for a white-box way to pick tests from the truth table of a decision.

See also Mutation Testing, where a boundary mutant survives until a test at the boundary value kills it.

See also a Scrum Sprint and its burndown chart, where an item counts only when it meets the Definition of Done, tests included.

What the page leaves out

Boundary tests on two inputs at once (each input at its six values while the other stays in the middle), classes of outputs, classes of strings, dates and other non-numbers, decision tables, state-transition testing, pairwise testing for many inputs, and the two-value variant of boundary analysis (only min − 1, min, max, max + 1) are left out. The page also never runs real code: the five variants of canOrder are fixed.

On the mind map

Open the Software Design mind mapSoftware DesignPrinciples: keep each change localPrinciplesDesign patterns: named fixes for common designsDesign patternsArchitecture: the shape of a whole appArchitectureEnterprise app patterns: logic, SQL, locks, money (PoEAA)Enterprise app patternsGit storage: content named by its hashGit storageGit branches & merging: pointers to commits, then mergeGit branches & mergingTesting: check code in isolationTestingSOLIDSOLIDfive rules so one change hits one classInheritance vs compositionInheritance vs compositionis-a locks the tree; has-a mixes partsVirtual calls, vtableVirtual calls, vtableobject → vptr → vtable slot → functionOverloading vs overridingOverloading vs overridingjavac: static types; JVM: receiver's classCoupling and cohesionCoupling and cohesionOne change should touch one moduleControl-flow coverageControl-flow coverage100% statements can still miss a bugBoundary Value TestingBugs sit at class edges: test min−1 … max+1Condition Coverage and MC/DCCondition Coverage and MC/DCEach condition flips D alone: N+1 testsMutation testingMutation testingCoverage runs code; mutants test the assertsOpen the Software Design mind mapSoftware Designkeep each change localPrinciplesSOLIDSOLIDfive rules so one change hits one classInheritance vs compositionInheritance vs compositionis-a locks the tree; has-a mixes partsVirtual calls, vtableVirtual calls, vtableobject → vptr → vtable slot → functionOverloading vs overridingOverloading vs overridingjavac: static types; JVM: receiver's classCoupling and cohesionCoupling and cohesionOne change should touch one moduleControl-flow coverageControl-flow coverage100% statements can still miss a bugBoundary Value TestingBugs sit at class edges: test min−1 … max+1Condition Coverage and MC/DCCondition Coverage and MC/DCEach condition flips D alone: N+1 testsMutation testingMutation testingCoverage runs code; mutants test the asserts
This page on the Software Design mind map · 38 pages in 7 branches · open it to see the whole map and test yourself in recall mode.