software-engineering testing

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:

  1. Input Variables: Explore types and ranges of individual inputs (unidimensional partitioning).
  2. Interactions: How inputs depend on or constrain each other (multidimensional partitioning).
  3. 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 ().
  • Objects: Valid vs. Invalid state.

Partition Types

To ensure complete coverage, partitions are categorised into two types:

  1. Valid Partitions: Values that should be accepted by the component (represent normal operation).
  2. 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:

IDarr1arr2Partition / RationaleExpected
T1nullnull (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]
T6null[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 IllegalArgumentException if null or 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.
  • 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):

IDInput pwdPartition / RationaleExpected Result
T1nullIllegalArgumentException
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):

IDWeightSpeedPartition / RationaleExpected Outcome
T10"standard" (invalid weight)IllegalArgumentException
T210null (null speed)IllegalArgumentException
T310"fast" (unknown speed)IllegalArgumentException
T42"standard"5.0
T55"express"7.5
T610"STANDARD" (case-insensitive)10.0
T721"Express"30.0