# Equivalence Class Partitioning

Let us say, you are the chief chef in a seven star hotel and you have to check the quality of the cooked food. You are presented the food as in the picture below. What will you do?

Definitely, you will not eat up all sweets to test its quality. You will take just one or two bites from every dish, or in other words, you will just taste everything. You will not eat it all. The reason is, it is not possible either.

The same analogy can applied on software testing. It is practically not possible to try every possible input for any given software. Therefore, we use the strategy of tasting. We divide the given testing space in different small categories and then write one test case for each category just like the whole dinner is categorized in different dishes and you take just one or two bites from each dish۔

## Writing test case for Equivalence Class Partitioning

1. Divide the given testing space into small classes.
2. Write a test case for each class.

In this picture, the blue ball on the left represents the whole testing space. You cannot try each input. So what we do, we divide the space into small chunks as in the ball on the right. Now there are total 7 chunks. These chunks are called equivalent classes and we write a test case from each equivalent class.

## Types of Equivalent classes

There are two types of equivalent classes.

### Boolean Class

For some scenario, we can create just two classes. One for valid input and the other invalid input. These are called Boolean class.

Example: say there is a text field in a web form, which accepts numeric values only. So there are just two classes, the class for valid input i.e. the numeric values and an invalid input class where we try the non-numeric values.

### Classes with Range of inputs

These are the equivalent classes where we have a range of valid inputs. In this case, we have a valid input class and two invalid input classes.

Example: Say there is a job where the age of the applicant should be greater than 22 and less than 40. Here, the class of inputs from 22 to 40 is a class with valid inputs. The class of inputs less than 22 and the class of inputs greater than 40 are equivalent classes with invalid inputs.

# Example of test cases with equivalent class partitioning

Say you are filling an online web form with following input fields.

Enter your name _______________________ alphabets only

Enter your CNIC _______________________ without dashes, numeric input only

Enter your age _________________________ Age must be within 22 and 40

Enter you weight _______________________ Weight must be within 50 and 120

Submit Button

First, let us figure out the equivalent classes for each input field.

1. Name field has two equivalent classes. One valid class for alphabetic input and one invalid class for non-alphabetic input.
2. CNIC field also has two equivalent classes. One valid class for numeric inputs and one invalid class for non-numeric inputs.
3. Age field has a condition where a range of valid input exists. Here we have three equivalent classes. One equivalent class with valid inputs. One equivalent class with invalid inputs below the range and one equivalent class with invalid inputs above the range.
4. The fourth field weight also has a range of valid inputs. Here again we have a valid equivalent class and two invalid equivalent classes, one above the valid range and the other below the valid range.

In short, we have two inputs each for first two fields and three inputs each for last two fields.

## Writing test cases for Equivalent Classes.

The best strategy to write test cases for a group of field is based upon the idea of bug segregation. We give invalid input for one input field only at a time. For all the rest of the fields, we enter the valid inputs. Following this criterion, let us write test cases for above example.

 Id Input Expected Output Actual Output Status 1 Invalid Name input, All the rest valid inputs 2 Invalid CNIC input, All the rest valid inputs 3 Invalid Age input from below the valid range, All the rest valid inputs. 4 Invalid Age input from above the valid range, All the rest valid inputs. 5 Invalid Weight input from below the valid range, All the rest valid inputs. 6 Invalid Weight input from above the valid range, All the rest valid inputs.