Mutation Testing – Learn This Interesting Testing Technique Quickly with a Simple Example
Mutation testing is one of the newly developed approaches to test a software application by deriving and using the better quality of test cases. The purpose of mutation testing is to evaluate the effectiveness of the test cases to detect errors in the event of modification or changes in the program code. However, these changes are very small so that it does not affect the overall quality of the application program.
The changes introduced or injected in the program code are generally referred as ‘mutants’. These mutants are injected in the lines of code to replace some variables or operands or syntax or conditions or expressions or statements in order to introduce faults in the code.
Let see a simple example to understand the concept of mutant injection in the program code:
Original Program:
1-Read annual salary.
2-If annual salary > Rs.2.50 Lacs.
3-Income Tax = 10% of 2.50 lacs.
4- Endif.
Above given are the lines of code which is very easy to understand, thus not explaining them. Now, in the above-given program, we try to inject mutant. Let’s see some of them
Mutant Program-1:
1-Read annual salary.
2-If annual salary< Rs.2.50 Lacs.
3-Income Tax = 10% of Rs.2.50 Lacs.
4-Endif.
The original program has been changed to the mutant program by replacing the operator ’>’ with the mutant ‘<’. Further, more unique mutants can be injected to create more mutant programs. Let’s see how
Mutant Program-2:
1-Read annual salary.
2-If annual salary && Rs.2.50 Lacs.
3-Income Tax = 10% of Rs.2.50 Lacs.
4-Endif.
Note:- Invalid operator(&&) injected.
Mutant Program-3:
1-Read annual salary.
2-If annual salary > Rs.2.50 Lacs.
3-
4-End If.
Note:- Deleting the line of code/statement.
Mutant Program-4:
1-Read annual salary.
2-If annual salary > 2.75 lacs.
3-Income Tax = 10% of Rs. 2.50 lacs.
4-Endif.
Note:- Changing the value in the statement/line of code.
Now, How to do mutation testing?
We have one original program and its four mutant programs. Test cases with relevant sets of test data are executed over the original and mutant program.
If the results of these test cases are same, then it may be inferred that the test cases are well enough to detect the difference between original and mutant program, and thereby killing the mutant.
And if the results are not same, then it may be concluded that test cases lack to distinguish between original and mutant program, and mutant is still alive. Thus, test cases need to be improved to kill mutants.
Consider following test data for executing test cases over the original program and mutant program-4:
- 2.80.
- 2.60.
On feeding 2.80, both original and mutant program generate similar results, thus mutant is killed by the test case. However, with the test data value of 2.60, results will be same, thus mutant is alive and is not detected by the test case, thereby needs improvement in the test cases.
Similarly, executing the above-given test data over the original program and mutant program-2, which generates similar results under both test data values i.e. failure. This means that the test cases are quite effective to detect changes and kill mutants.
The above-stated process needs to be repeated for each different mutant program and for each different set of test data to evaluate and improve the effectiveness and quality of the test cases.
Conclusion:
Although mutation testing is a time-consuming process but is effective to detect loopholes and flaws in the programming code. However, instead of seeing it as a testing technique, mutation testing may be more seen as a test improvement methodology, which improves the effectiveness and quality of the test cases, which ensures good test coverage and subsequently the better test results.