Notes
The problem
Take the quantity box on a ShopRung product page. It could receive any whole number, negative numbers, zero, decimals like two point five, text, an empty value or a giant number. Trying them all is impossible, which is one of the seven testing principles. Trying random ones is a waste. We need a reason to pick each value. That reason is how the software is supposed to treat it.
- whole numbers: ..., -1, 0, 1, 2, ..., 10, 11, ..., 9999
- decimals: 2.5, 0.1
- text: "two", "", " "
- huge: 99999999999
- principle 2: exhaustive
- testing is impossible
- so which ones do we try?
The idea
An equivalence partition is a group of inputs that the requirement says must be handled the same way. If a quantity of four is accepted, there is no reason in the rules for five or seven to behave differently. So one test stands for the whole group. You trade thousands of tests for a few, on purpose, and you can explain why each one is there. You will also hear the name equivalence class. It means the same thing.
- a group of inputs (or outputs)
- that the requirement says
- must be handled the same way
- assumption: if 4 works, 5 and 7 work too
- one test per partition
- instead of one per value
- also called equivalence class
Find partitions
Read the rule. Cart two says each cart line has a quantity from one to ten. That draws two lines on the number line, and makes three partitions. Below one is invalid. One to ten is valid. Eleven and above is invalid. Pick one value from each, say zero, five and fifteen. The two invalid ones must be refused with the message: quantity must be between one and ten. Three tests, each with a reason.
... -1 0 | 1 ... 10 | 11 ... invalid | valid | invalid too low | | too high pick one from each: 0 → refused 5 → accepted 15 → refused both invalid: "Quantity must be between 1 and 10"
Invalid types
Invalid partitions also come from the type of the input, not only its size. A decimal like two point five, text like the word two, an empty value and a negative number are all different kinds of wrong, and the code may handle each in a different place. In ShopRung, sending a quantity of two point five to the API returns status four hundred with invalid quantity. Give each invalid partition its own test, so one error message cannot hide another.
- not a whole number 2.5
- not a number "two"
- empty ""
- negative -3
- ShopRung API: quantity 2.5 → 400 invalid_quantity
- each invalid partition gets
- its own test, one at a time
Search example
Now the ShopRung search. Rule search three says the text must be two to fifty characters after trimming the spaces. That gives three length partitions: zero or one character is refused, two to fifty is searched, fifty one and more is refused with its own message. Trimming adds a sneaky one: three spaces look like three characters but trim to none. And search two adds a partition for case: mug in capitals must find the same two mugs.
- length 0 to 1 invalid "m" → "Search needs at least 2 characters"
- length 2 to 50 valid "mug" → 2 products
- length 51 and up invalid "Search can be at most 50 characters"
- spaces only " " trims to 0
- case: "MUG" = "mug" (SRCH-2)
Several rules
A password in ShopRung has three rules: eight to sixty four characters, at least one letter, and at least one digit. Partition each rule. The valid case meets all of them, like abcdefg1. Then write one invalid case per rule, and break only that rule in each case: a short password, one with no digit, one with no letter, and one that is sixty five characters. If a case broke two rules at once, you would not know which check refused it.
- length < 8 | 8..64 | > 64
- letter has one | has none
- digit has one | has none
- valid: "abcdefg1"
- invalid: "abc1" (too short) "abcdefgh" (no digit) "12345678" (no letter) 65 characters
- one rule broken per case
How many cases
How many cases do you need? Valid partitions can share a case, when they do not interfere, because a passing case tells you all of them worked. Invalid partitions need one case each, with everything else valid, because the first error usually stops the processing and hides the rest. So for the password, that is one valid case plus four invalid ones: five cases instead of millions of possible strings.
- valid partitions: cover several in one case when they are independent
- invalid partitions: one per case, everything else valid
- minimum = max(valid groups) + number of invalid groups (for one field: 1 + invalids)
Output partitions
Partitions are not only about inputs. Look at what comes out. Shipping in ShopRung has three possible outputs: four ninety nine below seventy five dollars, free at seventy five and above, and zero for an empty cart. Make sure each output appears in at least one test. The same goes for messages. Every distinct error message the requirements promise is an output partition, and each one deserves a test that makes it appear.
- after-discount subtotal $0.01 .. $74.99 → $4.99 $75.00 and up → free empty cart $0.00 → $0.00
- check each output appears
- at least once: $4.99, Free, $0.00
- messages are outputs too
The catch
Here is the catch. Equivalence partitioning assumes the code treats every value in a group the same. Bugs love the edges between groups. Imagine a developer wrote quantity greater than one instead of greater than or equal to one. Your three cases, zero, five and fifteen, all pass. But a quantity of exactly one is wrongly refused, and nobody tested it. That is why partitioning is always paired with the next technique, boundary value analysis.
- code written: if (q > 1 && q <= 10) // bug
- EP picks 0, 5, 15: 0 refused ok 5 accepted ok 15 refused ok
- but 1 is wrongly refused → also test the boundaries
Rules of thumb
Some quick habits. A range gives one valid and two invalid partitions. A set of named values, like the order statuses, gives one partition per value. A must have rule gives has and has not. A format gives valid, wrong type, empty and too long. For partitioning alone, pick a value from the middle of each group. Write the partitions in a small table before writing cases. It makes the reasoning visible, and reviewers can spot a missing group.
- a range → 1 valid, 2 invalid
- a set of values → 1 per value (each order status)
- a must-have → has / has not a format → valid, wrong type, empty, too long
- pick a middle value for EP, edges for BVA
- write the partitions in a table before the cases
Common mistakes
Common mistakes. Testing only the valid partition. Putting two invalid values in one case. Forgetting empty input and input that is only spaces. Forgetting type partitions like text and decimals. Deriving partitions from what the screen happens to allow, instead of from the requirement. Remember that the ShopRung quantity box allows you to type eleven on purpose, so the server rule is what you test. Ignoring outputs. And stopping before boundary values.
- only valid partitions
- two invalid values in one test case
- forgetting empty and spaces
- forgetting type partitions (text, decimals)
- partitions from the UI, not the requirement
- skipping output partitions
- stopping at EP, no BVA
Say it like this
Interviewers ask for a definition and an example, often an age field from eighteen to sixty. Say: I split the inputs into groups the system should treat the same way, valid and invalid, and test one value from each. For age eighteen to sixty, the groups are below eighteen, eighteen to sixty, above sixty, plus non numbers and empty. Then add: I always combine it with boundary value analysis, because bugs sit at the edges.
- "I split the inputs into groups the system should
- treat the same way, valid and invalid, and test one value
- from each. For an age field
- of 18 to 60: below 18, 18 to 60, above 60, plus non-numbers
- and empty. I always add
- boundary values, because bugs
- sit at the edges."