Mutation Testing: Stuart Anderson
Mutation Testing: Stuart Anderson
Stuart Anderson
Stuart Anderson
Mutation Testing
c 2011
Overview
Mutation testing is a structural testing method, i.e. we use the structure of the code to guide the test process. We cover the following aspects of Mutation Testing: What is a mutation? What is mutation testing? When should we use mutation testing? Mutations Examples Mutation testing tools
Stuart Anderson Mutation Testing c 2011
What is a mutation?
A mutation is a small change in a program. Such small changes are intended to model low level defects that arise in the process of coding systems. Ideally mutations should model low-level defect creation.
Stuart Anderson
Mutation Testing
c 2011
Assumptions
The validity of this rests on many assumptions: That mutations are a good model for defects. That defects are usually independent That the construction of T is not inuenced by knowledge of the mutation process (i.e. we do not use knowledge of the mutation process to build tests that are better at nding defects generated by mutations than normal defects). If we are interested in making condent estimates of very low RDDs we will need very large numbers of mutants. For example, if our development process left us with 10 defects per kLoc before test and we want to be condent our RDD after test is lower that 0.1 per kLoC then we need to test many mutants to be condent of such an RDD estimate.
Stuart Anderson Mutation Testing c 2011
An Approach to Mutation
Ideally we need systematically to apply mutations to the program under test. This would involve some criterion of applicability. Usually we consider mutation operators in the form of rules that match a context and create some systematic mutation of the context to create a mutant. The simple approach to coverage is to consider all possible mutants but this may create a very large number of mutants (in the case of estimating RDDs the value and condence required of the estimated RDD would control the number of mutants to be generated). Mutation testing is best supported by tools because of the potentially very large numbers of mutations to be generated during testing.
Stuart Anderson Mutation Testing c 2011
Kinds of Mutation
Value Mutations: these mutations involve changing the values of constants or parameters (by adding or subtracting values etc), e.g. loop bounds being one out on the start or nish is a very common error. Decision Mutations: this involves modifying conditions to reect potential slips and errors in the coding of conditions in programs, e.g. a typical mutation might be replacing a > by a < in a comparison. Statement Mutations: these might involve deleting certain lines to reect omissions in coding or swapping the order of lines of code. There are other operations, e.g. changing operations in arithmetic expressions. A typical omission might be to omit the increment on some variable in a while loop. A wide range of mutation operators is possible...
Stuart Anderson Mutation Testing c 2011
Stuart Anderson
Mutation Testing
c 2011
10
Value Mutation
Here we attempt to change values to reect errors in reasoning about programs. Typical examples are: Changing values to one larger or smaller (or similar for real numbers). Swapping values in initialisations. The commonest approach is to change constants by one in an attempt to generate a one-o error (particularly common in accessing arrays). Coverage criterion: Here we might want to perturb all constants in the program or unit at least once or twice.
Stuart Anderson
Mutation Testing
c 2011
Example
11
Value Mutation
Stuart Anderson
Mutation Testing
c 2011
12
Decision Mutation
Here again we design the mutations to model failures in reasoning about conditions in programs. As before this is a very limited model of programming error really modelling slips in coding rather than a design error. Typical examples are: Modelling one-o errors by changing < to <= or vice versa (this is common in checking loop bounds). Modelling confusion about larger and smaller, so changing > to < or vice versa. Getting parenthesisation wrong in logical expressions e.g. mistaking precedence between && and || Coverage Criterion: We might consider one mutation for each condition in the program. Alternatively we might consider mutating all relational operators (and logical operators e.g. replacing || by && and vice versa)
Stuart Anderson Mutation Testing c 2011
Example
13
Decision Mutation
Stuart Anderson
Mutation Testing
c 2011
14
Statement Mutation
Here the goal is primarily to model editing slips at the line level these typically arise when the developer is cutting and pasting code. The result is usually omitted or duplicated code. In general we might consider arbitrary deletions and permutations of the code. Typical examples include: Deleting a line of code Duplicating a line of code Permuting the order of statements. Coverage Criterion: We might consider applying this procedure to each statement in the program (or all blocks of code up to and including a given small number of lines).
Stuart Anderson Mutation Testing c 2011
Example
15
Statement Mutation
Stuart Anderson
Mutation Testing
c 2011
16
Observations
Mutations model low level errors in the mechanical production process. Modelling design errors is much harder because they involve large numbers of coordinated changes throughout the program. Ensuring test sets satisfy coverage criteria are often enough to ensure they kill mutants (because mutants often do not make sense and so provoke a failure if they are ever executed). Black-box test sets are poorer at killing mutants wed expect this because black-box tests are driven more by the operational prole than by the need to cover statements. We could see mutation testing as a way of forcing more diversity on the development of test sets if we use a black-box approach as our primary test development approach.
Stuart Anderson Mutation Testing c 2011
17
18
MuJava
19
Stuart Anderson
Mutation Testing
c 2011
MuJava
20
Stuart Anderson
Mutation Testing
c 2011
MuJava
21
Stuart Anderson
Mutation Testing
c 2011
22
Summary
Mutation testing can be a useful addition to the test process. It is laborious and really requires tool assistance if it is to be cost-eective. Improving Residual Defect Density estimates requires very large numbers of mutants if we are to have condence in the results. Object Orientation has a wide range of structural and operational mutants that are specic to objects. Tools like mu-Java use features of Java to enable the ecient generation and test of mutants. Even with ecient techniques execution times can be very slow for large numbers of mutants.
Stuart Anderson Mutation Testing c 2011
23
Required Readings
Textbook (Pezz` e and Young): Chapter 16, Fault-Based Testing MuJava: An Automated Class Mutation System, Yu-Seung Ma, Je Outt and Yong-Rae Kwon. Journal of Software Testing, Verication and Reliability, 15(2):97-133, June 2005. http://dx.doi.org/10.1002/stvr.v15:2 A. Jeerson Outt, Ammei Lee, Gregg Rothermel, Roland H. Untch, and Christian Zapf. 1996. An experimental determination of sucient mutant operators. ACM Trans. Softw. Eng. Methodol. 5, 2 (April 1996), 99-118. http://dx.doi.org/10.1145/227607.227610
Stuart Anderson
Mutation Testing
c 2011