Thursday 18 July 2013

Equivalence Class Partitioning with example


What is "Equivalence Class Partitioning"?

We define "Equivalence Class Partitioning" as a method that can help you derive test cases. You identify classes of input or output conditions. The rule is that each member in the class causes the same kind of behavior of the system. In other words, the "Equivalence Class Partitioning" method creates sets of inputs or outputs that are handled in the same way by the application.

Another definition taken from Wikipedia:
"A technique in black box testing. It is designed to minimize the number of test cases by dividing tests in such a way that the system is expected to act the same way for all tests of each equivalence partition. Test inputs are selected from each class. Every possible input belongs to one and only one equivalence partition."
Why learn "Equivalence Class Partitioning"?

This method drastically reduces the number of test cases that are required to be tested because we don't have time, money or manpower to test everything. In addition, it can help you find many errors with the smallest number of test cases.
How to use "Equivalence Class Partitioning"?

There are 2 major steps we need to do in order to use equivalence class partitioning:
  • Identify the equivalence classes of input or output. Take each input's or output's condition that is described in the specification and derive at least 2 classes for it:
    • One class that satisfies the condition – the valid class.
    • Second class that doesn't satisfy the condition – the invalid class.
  • Design test cases based on the equivalence classes.
Example 1
In a computer store, the computer item can have a quantity between -500 to +500. What are the equivalence classes?
Answer: Valid class: -500 <= QTY <= +500
                Invalid class: QTY > +500
                Invalid class: QTY < -500

Example 2
In a computer store, the computer item type can be P2, P3, P4, and P5 (each type influences the price). What are the equivalence classes?
Answer: Valid class: type is P2
                Valid class: type is P3
                Valid class: type is P4
                Valid class: type is P5
                Invalid class: type isn’t P2, P3, P4 or P5
Practice
Bank account can be 500 to 1000 or 0 to 499 or 2000 (the field type is integer). What are the equivalence classes?
Try to solve it before reading the answer.
Practice 1 - answer
  • Valid class: 0 <= account <= 499
  • Valid class: 500 <= account <= 1000
  • Valid class: 2000 <= account <= 2000
  • Invalid class: account < 0
  • Invalid class: 1000 < account < 2000
  • Invalid class: account > 2000
Equivalence Class Vs Boundary Testing

Let us discuss about the difference between Equivalence class and boundary testing. For the discussion we will use the practice question:
Bank account can be integer in the following ranges: 500 to 1000 or 0 to 499 or 2000. What are the equivalence classes?
Answer:
  • valid class: 0 <= account <= 499
  • valid class: 500 <= account <= 1000
  • valid class: 2000 <= account <= 2000
  • invalid class: account < 0
  • invalid class: 1000 < account < 2000
  • invalid class: account > 2000
In equivalence class, you need to take one value from each class and test whether the value causes the system to act as the class' definition. It means that in this example, you need to create at least 6 test cases – one for each valid class and one for each invalid class.
How many test cases will be, if you use boundary testing?
The following table shows how much test cases will be using "Boundary Testing" method:
Test Case #
Value
Result
1
-1 Invalid
2
0 Valid
3
1 Valid
4
498 Valid
5
499 Valid
6
500 Valid
7
501 Valid
8
999 Valid
9 1000 Valid
10 1001 Invalid
11 1999 Invalid
12 2000 Valid
13 2001 Invalid

In boundary testing, you need to test each value in the boundary and you know the value, you don't need to choose it from any set. In this example you have 13 test cases.
Now, let us exam how to combine this 2 methods together.
The following table shows all the boundary testing values and their equivalence classes:
#
Boundary Value
Equivalence Class
Result
1
-1 account < 0 Invalid 
2
0 0 <= account <= 499 Valid 
3
1 0 <= account <= 499 Valid 
4
498 0 <= account <= 499 Valid 
5
499 0 <= account <= 499 Valid 
6
500 500 <= account <= 1000  Valid
7
501 500 <= account <= 1000 Valid 
8
999 500 <= account <= 1000 Valid 
9 1000 500 <= account <= 1000  Valid
10 1001 1000 < account < 2000 Invalid 
11 1999 1000 < account < 2000  Invalid
12 2000 2000 <= account <= 2000 Valid 
13 2001 account > 2000 Invalid 

Now, we can reduce some of the test cases that belong to the same equivalence class. We can delete lines 3 and 4 which belong to equivalence class "0 <= account <= 499". We also can delete lines 7 and 8 hich belong to "500 <= account <= 1000". The new table will be:
#
Boundary Value
Equivalence Class
Result
1
-1 account < 0 Invalid 
2
0 0 <= account <= 499 Valid 
5
499 0 <= account <= 499 Valid 
6
500 500 <= account <= 1000  Valid
9 1000 500 <= account <= 1000  Valid
10 1001 1000 < account < 2000 Invalid 
11 1999 1000 < account < 2000  Invalid
12 2000 2000 <= account <= 2000 Valid 
13 2001 account > 2000 Invalid 
You can even reduce more test cases although in my opinion, it is important to keep this table because it keeps a hard connection to the boundary testing. You can see in the table that I didn't reduce those test cases that are touch in the boundary itself of each range.
Let's reduce more test cases (just for the fun and for the practice (test case 5, 9 and 10):
#
Boundary Value
Equivalence Class
Result
1
-1 account < 0 Invalid 
2
0 0 <= account <= 499 Valid 
6
500 500 <= account <= 1000  Valid
11 1999 1000 < account < 2000  Invalid
12 2000 2000 <= account <= 2000 Valid 
13 2001 account > 2000 Invalid 
Now, in this table, for each equivalence class, you choose one value that belongs to boundary testing.

No comments:

Post a Comment