Testing 4 White
Testing 4 White
& Mutation
Filippo Ricca
DISI, Universit di Genova, Italy
[email protected]
Inputs
Outputs
Inputs
Called also functional testing
Usually testcases are generated starting from
requirements/specifications
Outputs
What is adequacy?
Test suite T
P
Fault detection
what if no faults are found?
How good this test suite is? Has P been
tested thoroughly?, or Is T adequate?
Two possibilities:
Test Coverage
Test Coverage
source code
model
requirements checklist
...
5
89%
Statement coverage
Branch coverage (also called decision coverage)
Minimum coverage specified by the IEEE unit test
standard
Multiple Condition coverage
Covers combinations of condition in decisions
Path coverage
100% path coverage impossible in practice
Loops ...
7
true
a > 1 AND
b=0
false
xx/a
4
a == 2
OR x>1
true
false
x x+1
7
8
Statement Coverage
Procedure:
Find paths that cover all statements
Choose input data that will result in the
selected paths
9
Statement Coverage
Possible input:
a = 2, b = 0, x = 4
true
a > 1 AND
b=0
false
xx/a
4
a == 2
OR x>1
true
false
x x+1
7
10
a > 1 AND
b=0
false
xx/a
4
a == 2
OR x>1
true
false
x x+1
7
11
Branch Coverage
Procedure:
Find paths that cover all branches
Choose input data that will result in the
selected paths
Branch coverage includes statement coverage!
12
Branch Coverage
a > 1 AND
b=0
false
xx/a
1.
2.
Possible input:
a = 2, b = 2, x = -1
a = 3, b = 0, x = 1
a == 2
OR x>1
true
true
false
x x+1
7
13
Criterion:
15
Possible input:
a = 2, b = 0,
a = 2, b = 1,
a = 0, b = 0,
a = 0, b = 1,
x
x
x
x
=
=
=
=
2
0
2
0
[1][5]
[2][6]
[3][7]
[4][8]
16
=
=
=
=
2,
2,
0,
0,
b
b
b
b
=
=
=
=
0,
1,
0,
1,
x
x
x
x
Paths covered
13457
12457
12457
12467
=
=
=
=
2
0
2
0
false
xx/a
4
a == 2
OR x>1
Equal
true
a > 1 AND
b=0
true
false
x x+1
7
17
Criterion:
18
=
=
=
=
0,
3,
2,
2,
b
b
b
b
=
=
=
=
false
xx/a
Input values:
a
a
a
a
1,
0,
1,
0,
x
x
x
x
true
a > 1 AND
b=0
=
=
=
=
0
0
0
0
a == 2
OR x>1
true
false
x x+1
7
19
Comparison
20
Infeasible paths
Set of paths:
12467
13467
12457
13457
To be able to take this path, we
would have to have:
1
true
a>1
false
xx/a
4
a>4
true
false
x x+1
7
21
Clover:
Emma:
http://coverlipse.sourceforge.net
Cobertura:
http://emma.sourceforge.net
http://www.eclemma.org
Coverlipse:
http://www.cenqua.com/clover
http://cobertura.sourceforge.net
....
22
Emma
Open-source tool
Standalone version
http://emma.sourceforge.net
24
25
EMMA example
public class Calc {
int tot;
Calc(){
tot=0;
}
int somma (int x) {
tot=tot + x;
return tot;
}
int sottrai (int x) {
tot=tot - x;
return tot;
}
int moltiplica (int x) {
tot=tot * x;
return tot;
}
}
Output EMMA
Coverage = 100%
Program Mutation
29
30
Mutation terms
Es. + --> *
Live otherwise
31
Mutation operator
32
Method-level operator
(examples)
Mutant 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;
33
Class-level operator
(examples)
34
Mutant score
Mutant killed: if its behaviors/outputs differ from those of the original program
testcase
35
Mutants equivalent to P!
36
Computing Adequacy
Foo
Foo
Foo
MS(FooTest)=?
Foo
Tools Usage
Mutating Foo
Tests: FooTest
Mutation points = 12, unit test time limit 2.02s
.. M FAIL: Foo:31: negated conditional
M FAIL: Foo:33: negated conditional
M FAIL: Foo:34: - -> +
M FAIL: Foo:35: negated conditional
......
Score: 67%
37
39
Foo (1)
Correct?
40
int foo(int x, y) {
return (x-y);
}
Foo (2)
M1:
int foo(int x, y) {
return (x+y);
}
mutants
Testsuite
is
not
adequate!
Next we execute each mutant against tests in T
MS(T)= 1/3
T={ t1: <x=1, y=0>, t2: <x=-1, y=0>}
Test (t)
foo(t)
M1(t)
M2(t)
M3(t)
t1
t2
-1
-1
-1
Live
Live
Killed
41
Foo (3)
int foo(int x, y) {
return (x-y);
}
Let us examine M1
A test that distinguishes M1 from foo must satisfy the
following condition:
x-yx+y => -yy => -2y 0 => y 0
mutation
following
MuClipse
Others at:
http://www.mutationtest.net/twiki/bin/view/Resources/WebHome
44
Jumble installation
1.
2.
3.
4.
http://jumble.sourceforge.net
Restart Eclipse
45
If :
class to be mutated
then you
Jumble
1.
2.
3.
can
run
class "Foo
JUnit tests "FooTest"
Jumble starts by running the unit tests (in
FooTest.class) on the unmodified Foo class to check
that they all pass, and to measure the time taken by
each test
Then it will mutate Foo in various ways and run the
tests again to see if they detect the mutation
It continues this process until all mutations of Foo
have been tried
It provides the output
47
Jumble Output
Jumble found 12 different mutants of Foo
Mutating Foo
Tests: FooTest
Mutation points = 12, unit test time limit 2.02s
.. M FAIL: Foo:31: negated conditional
M FAIL: Foo:33: negated conditional
M FAIL: Foo:34: - -> +
M FAIL: Foo:35: negated conditional
......
Score: 67%
if (C) decision on line 31 was mutated to if (!C)
unit tests kill the mutant (indicated by a '.')
48
Jumble mutations
+ --> -, * --> /
swapping
49
Jumble Example
public class CalcTest {
Calc c;
public class Calc {
int tot;
Calc(){
tot=0;
}
int somma (int x) {
tot=tot + x;
return tot;
}
int sottrai (int x) {
tot=tot - x;
return tot;
}
int moltiplica (int x) {
tot=tot * x;
return tot;
}
}
@Before
public void setUp(){
c= new Calc();
}
@Test
public void testSomma() {
Assert.assertEquals(c.somma(3), 3);
}
@Test
public void testSottrai() {
Assert.assertEquals(c.sottrai(0), 0);
}
@Test
public void testMoltiplica() {
c.somma(2);
Assert.assertEquals(c.moltiplica(3), 6);
}
}
Jumble output
Mutating Calc
Tests: CalcTest
Mutation points = 7, unit test
time limit 2.31s
...M FAIL: Calc:15: - -> +
...
Score: 85%
51
Test enhancement
Mutating Calc
Tests: CalcTest
Mutation points = 7, unit test
time limit 2.31s
.......
Score: 100%
http://www.site.uottawa.ca/~awilliam/seg3203/Coverage.ppt
Ex. Foo
53
References
http://www-128.ibm.com/developerworks/java/library/j-cq01316
http://www.cs.purdue.edu/homes/apm/foundationsBook/
InstructorSlides.html
http://selab.fbk.eu/swat/program.ml?lang=en
Wikipedia
54