Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use...

29
Mutation Testing G. Rothermel

Transcript of Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use...

Page 1: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Mutation Testing

G. Rothermel

Page 2: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Fault-Based Testing

White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing what we hope will be a test suite that is effective at revealing faults.

Fault-based testing instead aims to detect certain classes of known faults

Example: security testing for buffer overflows

Page 3: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Mutation Testing

Fault-based testing strategy Used to evaluate test suite adequacy Measures the effectiveness of test cases Leads to creation of more effective tests

Page 4: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Mutation Testing

Faults are introduced into the program by creating versions of the program called mutants

Each mutant contains a single fault Test cases are applied to the original program

and to each mutant Goal is to cause the mutants to fail, thus

demonstrating the effectiveness of the test cases

Page 5: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Mutation Testing

Suppose that program P has been tested against a test set T and P has not failed on any test case in T. Now suppose we do the following:

Changed to

P P’

What behavior do you expect from P’ against tests in T?

Page 6: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Mutation Testing

P’ is known as a mutant of P.

There might be a test t in T such that P(t)≠P’(t). In this case we say that t distinguishes P’ from P. Or, that t has killed P’.

There might be not be any test t in T such that P(t)≠P’(t). In this case we say that T is unable to distinguish P and P’. Hence P’ is considered live in the test process.

Page 7: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Mutation Testing

If there does not exist any test case t in the input domain of P that distinguishes P from P’ then P’ is said to be equivalent to P.

If P’ is not equivalent to P but no test in T is able to distinguish it from P then T is considered inadequate.

A non-equivalent and live mutant offers the tester an opportunity to generate a new test case and hence enhance T.

Page 8: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Mutation Testing

Original Program

Mutants

M1

M2

M3

Mi

T1 T2 T3 T4 T5 ... Tj

X X

X

X

killed

killed

killed

live

mutation program

Page 9: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Example of a Program Mutation

1 int max(int x, int y)

2 {

3 int mx = x;

4 if (x > y)

5 mx = x;

6 else

7 mx = y;

8 return mx;

9 }

1 int max(int x, int y)2 {3 int mx = x;4 if (x < y)5 mx = x;6 else7 mx = y;8 return mx;9 }

Page 10: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Mutation Score

The mutation score for a test suite T: % of non-equivalent mutants killed by the tests in T

Mutation Score = 100 *D / (N – E) D = number of killed mutants N = total number of mutants E = number of equivalent mutants

Test suite T is mutation-adequate if its mutation score is 100%

Page 11: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Distinguishing a Mutant

A test case t that distinguishes a mutant M from its parent program P must satisfy 3 conditions:

Condition 1: Reachability: t must cause m to follow a path that arrives at the mutated statement in M.

Condition 2: Infection: must cause the sate of M and P to differ consequent to some execution of the mutated statement.

Condition 3: Propagation: must ensure that the difference in the states of M and P, created due to the execution of the mutated statement, propagates until after the end of mutant execution.

Page 12: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Equivalent Mutants

• The problem of deciding whether or not a mutant is equivalent to its parent program is undecidable. There is no way to automate detection of equivalent mutants.

• The number of equivalent mutants can vary from one program to another.

• Empirical studies have shown that one can expect about 5% of the generated mutants to be equivalent mutants.

• Identifying equivalent mutants is generally a manual and often time consuming---as well as frustrating---process.

Page 13: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Test Adequacy using Mutation

Given a test set T for program P that must meet requirements R, a test adequacy assessment procedure proceeds as follows.

Step 1: Create a set M of mutants of P. Let M={M1, M2…Mk}. Note that we have k mutants.

Step 2: For each mutant Mi determine whether there exists a test t in T such that Mi(t)≠P(t). If such a t exists then Mi is considered killed and removed from further consideration.

Page 14: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Test Adequacy using Mutation

Step 3: At the end of Step 2 suppose that k1 ≤ k mutants have been killed and (k-k1) mutants are live.

Case 1: (k-k1)=0: T is adequate with respect to mutation.

Case 2: (k-k1)>0 then we compute the mutation score (MS) as follows:

MS=k1/(k-e)

Where e is the number of equivalent mutants. Note: e ≤ (k-k1).

Page 15: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Test Enhancement using Mutation

One has the opportunity to enhance a test set T after having assessed its adequacy.

Step 1: If the mutation score (MS) is 100%, then some other technique, or a different set of mutants, needs to be used to help enhance T.

Step 2: If the mutation score (MS) is less than 100%, then there exist live mutants that are not equivalent to P. Each live mutant needs to be distinguished from P.

Page 16: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Test Enhancement using Mutation

Step 3: Hence a new test t is designed with the objective of distinguishing at least one of the live mutants; let us say this is mutant m.

Step 4: If t does not distinguish m then another test t needs to be designed to distinguish m. Suppose that t does distinguish m.

Step 5: It is also possible that t also distinguishes other live mutants.

Page 17: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Test Enhancement using Mutation

Step 6: One now adds t to T and re-computes the mutation score (MS).

Repeat the enhancement process from Step 1.

Page 18: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Competent Programmer Hypothesis (CPH)

• CPH states that given a problem statement, a programmer writes a program P that is in the general neighborhood of the set of correct programs.

• An extreme interpretation of CPH is that when asked to write a program to find the account balance, given an account number, a programmer is unlikely to write a program that deposits money into an account.

Page 19: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

• A more reasonable interpretation of the CPH is that the program written to satisfy a set of requirements will be a few mutants away from a correct program.

• The CPH assumes that the programmer knows of an algorithm to solve the problem at hand, and if not, will find one prior to writing the program.

Competent Programmer Hypothesis (CPH)

Page 20: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

• The mutants simulate the likely effect of real faults.

• Therefore, if the test set is good at catching the artificial mutants, it will also be good at catching the real faults in our program

Competent Programmer Hypothesis (CPH)

Page 21: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Coupling Hypothesis

• If there are any large and dramatic effects that arise from bugs in the software then these will be closely coupled to small and simple bugs.

• The net effect of these assumptions is that we take on faith the basic principle of mutation testing: making small mutations to the program code emulates the real bugs in the software.

Page 22: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Mutation Operators

• A mutation operator O is a function that maps the program under test to a set of k (zero or more) mutants of P.

O(P)

M1

M2

Mk

….

Page 23: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Mutation Operators

• A mutation operator creates mutants by making simple changes in the program under test.

• For example, the “variable replacement” mutation operator replaces a variable name by another variable declared in the program. A “relational operator replacement” mutation operator replaces relational operator with another relational operator.

Page 24: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Mutation Operators: Goodness

• The design of mutation operators is based on guidelines and experience.

• It is thus evident that two groups might arrive at a different set of mutation operators for the same programming language.

• How should we judge whether or not that a set of mutation operators is “good enough?”

Page 25: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Mutation Operators: Goodness

• Informal definition: • Let S1 and S2 denote two sets of

mutation operators for language L. • We say that S1 is superior to S2 if

mutants generated using S1 guarantee a larger number of errors detected over a set of erroneous programs.

Page 26: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Mutation Operators: Goodness

• Generally one uses a small set of highly effective mutation operators rather than the complete set of operators.

• Experiments have revealed relatively small sets of mutation operators for C and Fortran. We say that one is using “constrained” or “selective” mutation when one uses this small set of mutation operators.

Page 27: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Mutation Operators: Language Dependence

• For each programming language one develops a set of mutation operators.

• Languages differ in their syntax thereby offering opportunities for making mistakes that duffer between two languages. This leads to differences in the set of mutation operators for two languages.• Mutation operators have been developed for languages such as Fortran, C, Ada, and Java.

Page 28: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Mutation Operators: Examples

Mutation operator In P In mutant

Variable replacement

z=x*y+1; x=x*y+1;z=x*x+1;

Relational operator replacement

if (x<y) if(x>y)if(x<=y)

Off-by-1 z=x*y+1; z=x*(y+1)+1;z=(x+1)*y+1;

Replacement by 0 z=x*y+1; z=0*y+1;z=0;

Arithmetic operator replacement

z=x*y+1; z=x*y-1;z=x+y-1;

Page 29: Mutation Testing G. Rothermel. Fault-Based Testing White-box and black-box testing techniques use coverage of code or requirements as a “proxy” for designing.

Experiences

Theoretical and experimental results have shown that mutation testing is effective to measure adequacy of test cases

The major drawbacks of mutation testing are the cost of generating the mutants and executing each test case against them, and the cost of determining equivalent mutants

Mutations have also been used in experimental evaluations of testing techniques