# Equivalence partition example

Ir para: navegação, pesquisa

Consider a human-resource system that decides how we should process employment applications based on a person’s age.

 Age range Decision 0 - 16 Do not hire. 16 - 18 Can hire on a part-time basis only. 18 - 55 Can hire as a full-time employee 55 - 99

How the test cases for the above example should be designed? We could test the module for the following ages: 0, 1, 2, 3, 4, 5, 6, 7, 8, ..., 90, 91, 92, 93, 94, 95, 96, 97, 98, 99? Sure! But we could just one element from each range. Actually, each range is a equivalence class, so any one within that range is just as good as any other one. Indeed, considering the following implementation, this assumption is valid:

```if (applicantAge >= 0 && applicantAge <= 16)
hireStatus = "NO";
if (applicantAge >= 16 && applicantAge <= 18)
hireStatus = "PART";
if (applicantAge >= 18 && applicantAge <= 55)
hireStatus = "FULL";
if (applicantAge >= 55 && applicantAge <= 99)
hireStatus = "NO";
```

Using the equivalence partition criterion, we have reduced the number of test cases from 100 (testing each age) to four (testing one age in each equivalence class) – a significant savings (96%).

However, what if the module implementation is:

```if (applicantAge == 0) hireStatus = "NO";
if (applicantAge == 1) hireStatus = "NO";
...
if (applicantAge == 15) hireStatus = "NO";
if (applicantAge == 16) hireStatus = "PART";
if (applicantAge == 17) hireStatus = "PART";
if (applicantAge == 18) hireStatus = "FULL";
if (applicantAge == 19) hireStatus = "FULL";
...
if (applicantAge == 53) hireStatus = "FULL";
if (applicantAge == 54) hireStatus = "FULL";
if (applicantAge == 55) hireStatus = "NO";
if (applicantAge == 56) hireStatus = "NO";
...
if (applicantAge == 98) hireStatus = "NO";
if (applicantAge == 99) hireStatus = "NO";
```

Given this implementation, the fact that any set of tests passes tells us nothing about the next test we could execute. It may pass; it may fail. Thus, the equivalence partition testing assumption does not apply.

Another possibility is the non-conformity of the implementation to the specification:

```if (applicantAge >= 0 && applicantAge <= 16)
hireStatus = "NO";
if (applicantAge >= 16 && applicantAge <= 18)
hireStatus = "PART";
if (applicantAge >= 18 && applicantAge <= 41)
hireStatus = "FULL";
// strange statements follow
if (applicantAge == 42 && applicantName == "Lee")
hireStatus="HIRE NOW AT HUGE SALARY";
if (applicantAge == 42 && applicantName <> "Lee")
hireStatus="FULL";
// end of strange statements
if (applicantAge >= 55 && applicantAge <= 99)
hireStatus = "NO";
```