Definition
Equivalence partitioning is a test-design method that divides the possible inputs of a program into classes, called equivalence classes or partitions, such that the specification requires the same behavior for every member of a class, and then tests one or a few members of each class. The classes do not overlap, and together they contain every input. The method rests on an assumption, not a fact: that the program treats every member of a class alike, so that one passing member stands for the rest.
The idea
A function receives a 16-bit length and must accept the lengths 0 to 16 and refuse the lengths 17 to 65535. There are 65,536 possible lengths, and a real interface has several such inputs at once, so testing every combination is usually out of reach. The specification, though, makes only two promises about this input: accept a short length, refuse a long one. Equivalence partitioning turns those promises into two classes and tests each class.
The saving rests on a bet. A test of length 7 says something about length 12 only if the program handles 7 and 12 the same way. The specification says that the program should; whether it does depends on how the program was written. What justifies the bet is evidence about the program, not the specification alone.
How it works
List the dimensions. Each input field and each piece of state that affects the behavior, such as the current mode of a device, is a dimension.
Divide each dimension by specified behavior. Make one class for each behavior the specification gives. Make a separate invalid class for each distinct reason for refusal, such as a length that is too long, an unsupported version or a wrong authentication §1.6 tag, because each reason is checked by different code.
Choose representatives. One test may cover a valid class from each of several dimensions at once. Each invalid class gets a test of its own, with every other input valid. The classic description of the method, in Glenford Myers’s book The Art of Software Testing (1979), gives this rule, and the reason is masking: if one test held two invalid values, the refusal caused by one could hide a missing check for the other.
Add the edges. Off-by-one errors sit at the edges of the classes, so add boundary-value testing §10.31, which tests the values at and next to each edge, for every class whose values have an order.
Record why each class is a class. Note whether the class comes from the specification alone, or whether the code was also examined and found to treat all its members the same way. Split a class wherever the code treats its members differently.
Worked example
The specification accepts a 16-bit length when and refuses it otherwise. The classes are , with 17 members, and , with members. Together they hold values, every possible length. The tests use the representatives 7 from and 1000 from .
The implementation, written in C, first copies the length into a variable m of type uint8_t, the 8-bit unsigned integer type, with the statement uint8_t m = n;, and then accepts when m <= 16. C converts a value to an 8-bit unsigned type by keeping its remainder after division by 256. For 7, and the length is accepted, which is correct. For 1000, , so and the length is refused, which is also correct. Both tests pass.
Yet the program is wrong for part of . Length 256 gives , and length 272 gives ; both are accepted. A length is wrongly accepted when it is at least 256 and its remainder after division by 256 is between 0 and 16. That is 17 values in each of the 255 ranges that start at , so wrongly accepted lengths. A member of chosen at random exposes the error with probability , about 1 in 15.
Class is one class in the specification but two classes in this program: lengths whose remainder after division by 256 is above 16, which are refused correctly, and lengths whose remainder is 16 or less, which are accepted wrongly. A tester who reads the code sees the 8-bit variable, splits at the internal limit between 255 and 256, and tests 256, which fails.
Stated precisely
Let be the set of all inputs and the specification. Call two inputs equivalent when requires the same behavior for both. This relation is reflexive, symmetric and transitive, so its classes are non-empty, do not overlap, and together make up ; that is what a partition of means. A test picks a representative from each class .
For a program , call a class revealing when either behaves correctly on every member of or behaves incorrectly on every member of . Elaine Weyuker and Thomas Ostrand introduced this notion in 1980 [493]. If every class is revealing, then is correct on all of exactly when is correct on . Whether a class is revealing depends on , the program under test, so a partition taken from alone cannot guarantee it. Testing theory calls the assumption that each class is revealing the uniformity hypothesis.
Three kinds of evidence support that assumption, in increasing strength.
The specification, which says that the members should be treated alike, and says nothing about the code.
An inspection of the code, which shows that every member follows the same path through the program and that no step on that path depends on the exact value in a way the class ignores.
A proof, or an exhaustive check of the class, which shows that is correct on every member.
Only the third makes the result hold for every member; the class has then, in effect, been checked completely.
Limits and common mistakes
A test of one representative is one observation. It shows that one member is handled correctly, not that the class is.
Classes formed for one property need not hold for another. All refused requests may return the same error code, yet differ in the time they take or the memory they leave in use. A partition chosen for the error code says nothing about time or memory.
Classes for each input separately do not cover interactions. Two inputs with three classes each can be covered by three tests, one class of each input per test, but the combinations can behave differently.
Special values inside a class, such as a length of 0 that skips a copy, form classes of their own in the implementation even when the specification treats them like their neighbors.
Questions answered
What justifies treating all members of a class as equivalent?
Only evidence that the program behaves the same way, for the property being tested, on every member of the class justifies it; the specification alone does not. An equivalence class is a set of inputs for which the specification requires the same behavior, such as all lengths from 17 to 65535, which must be refused. The specification shows that the members should be treated alike. Whether the program does treat them alike depends on its code: a program that copies a 16-bit length into an 8-bit variable refuses 1000 but accepts 256, which splits one specified class in two.
The evidence, in increasing strength, is: reading the code and finding that every member follows the same path, with no step that depends on the exact value in a way the class ignores; then a proof, or an exhaustive check of the whole class. Without such evidence, treating the class as equivalent is an assumption, called the uniformity hypothesis, and a test report should say so. State the property the class was formed for, split the class wherever the code distinguishes its members, and add boundary values at its edges.
Related entries: Boundary-value testing (§10.31), Exhaustive enumeration (§10.33), Model-based testing (§10.34), Test oracle (§10.5), Integer promotion and conversion (§4.18). Further reading: [493].