Definition
Equivalence Partitioning
Equivalence Partitioning is a specification-based testing technique that divides the input data of a software unit into partitions of equivalent data from which test cases can be derived.
The fundamental premise is that if one test case in a partition detects a defect, other cases in the same partition would likely detect the same defect.
Identification
A systematic way to identify partitions is to look at:
- Input Variables: Explore types and ranges of individual inputs (unidimensional partitioning).
- Interactions: How inputs depend on or constrain each other (multidimensional partitioning).
- Outputs: Identify all possible types of outputs and work backwards to find inputs that trigger them.
Common Partition Categories
Standard partitions often include:
- Numerical: Ranges (e.g., , , ).
- Collections/Strings:
- : Input is
null. - : Input is empty ().
- : Input is populated ().
- : Input is
- Objects: Valid vs. Invalid state.
Partition Types
To ensure complete coverage, partitions are categorised into two types:
- Valid Partitions: Values that should be accepted by the component (represent normal operation).
- Invalid Partitions: Values that should be rejected by the component (represent error handling/exceptions).
Example
Example: alternateMerge
int[] alternateMerge(int[] arr1, int[] arr2) { /* ... */ }Specification: Combines two integer arrays by interleaving their elements in alternating order. If one array runs out, the remaining elements of the other are appended. If either array is null, it is treated as empty. Returns a new, non-null array.
arr1: first integer array, may be null.arr2: second integer array, may be null.
1. Input Partitions (Individual Variables): We identify partitions for each array independently:
- : Input is
null. - : Input is
[]. - : Input is populated ().
2. Input Partitions (Interactions): We analyse how the two inputs relate to each other (specifically length, as “running out” is key behaviour):
- : Lengths are equal.
- : First array is longer (append tail).
- : Second array is longer (append tail).
- Validity: Combinations of Null/Empty/Populated (e.g., vs ).
3. Test Cases (Derived): We select representatives that cover the identified partitions and interactions:
| ID | arr1 | arr2 | Partition / Rationale | Expected |
|---|---|---|---|---|
| T1 | null | null | (Both null treated as empty) | [] |
| T2 | [] | [] | [] | |
| T3 | [1, 2] | [3, 4] | , (Standard Interleave) | [1, 3, 2, 4] |
| T4 | [1, 2, 3] | [4, 5] | , (Append arr1) | [1, 4, 2, 5, 3] |
| T5 | [1, 2] | [3, 4, 5] | , (Append arr2) | [1, 3, 2, 4, 5] |
| T6 | null | [1] | (Treat null as empty) | [1] |
Example: passwordStrength
String passwordStrength(String pwd) { /* ... */ }Specification: Evaluates password strength based on length and composition. Returns “Weak”, “Medium”, or “Strong”.
- Exceptions: Raises
IllegalArgumentExceptionifnullor empty. - Rules:
- Length < 6: “Weak”.
- Length >= 6: Check for Digit AND Uppercase.
- Both: “Strong”.
- One missing: “Medium”.
- Both missing: “Weak”.
1. Input Partitions: We break down the input domain based on the specification rules:
- Validity (Pre-condition):
- : Input is
null. - : Input is empty string
"". - : Input is a non-empty string.
- : Input is
- Length (if Valid):
- : Length < 6.
- : Length >= 6.
- Content (if Valid & Long):
- : No Digit, No Upper.
- : Digit, No Upper.
- : No Digit, Upper.
- : Digit AND Upper.
2. Test Cases (Derived):
| ID | Input pwd | Partition / Rationale | Expected Result |
|---|---|---|---|
| T1 | null | IllegalArgumentException | |
| T2 | "" | IllegalArgumentException | |
| T3 | "abc" | "Weak" | |
| T4 | "abcdef" | "Weak" | |
| T5 | "1bcdef" | "Medium" | |
| T6 | "Abcdef" | "Medium" | |
| T7 | "A1cdef" | "Strong" |
Example: shippingCost
double shippingCost(int weight, String speed) { /* ... */ }Specification: Computes cost from weight and speed. Throws exception for invalid inputs.
- Weights: , , .
- Speed: “standard” (), “express” (). case-insensitive.
1. Input Partitions:
- Weight:
- : (invalid)
- : (base 5)
- : (base 10)
- : (base 20)
- Speed:
- : Invalid (null, empty, unknown).
- : “standard” (case-insensitive).
- : “express” (case-insensitive).
2. Test Cases (Derived):
| ID | Weight | Speed | Partition / Rationale | Expected Outcome |
|---|---|---|---|---|
| T1 | 0 | "standard" | (invalid weight) | IllegalArgumentException |
| T2 | 10 | null | (null speed) | IllegalArgumentException |
| T3 | 10 | "fast" | (unknown speed) | IllegalArgumentException |
| T4 | 2 | "standard" | 5.0 | |
| T5 | 5 | "express" | 7.5 | |
| T6 | 10 | "STANDARD" | (case-insensitive) | 10.0 |
| T7 | 21 | "Express" | 30.0 |