Synthesize it: from Design by Contract to Meaningful Test ...
Transcript of Synthesize it: from Design by Contract to Meaningful Test ...
Professor Horst Cerjak, 19.12.2005
1
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Synthesize it: from Design by Contract™
to Meaningful Test Input Data
Stefan J. Galler, Martin Weiglhofer and
Franz Wotawa
Professor Horst Cerjak, 19.12.2005
2
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Synthia generates jUnit tests that use
automatically synthesized Fake objects
instead of real objects as parameters
CUT
+
DbC
Synthia
Fake
jUnit
Professor Horst Cerjak, 19.12.2005
4
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Synthia generates test data based on Design
by Contract specification
White
Box PEX, JPF
Design by
Contract Spec Explorer PEX, jTest
Black
Box
TGV, Overture
(VDM) ARTOO, JET
Test
sequence
Test
data
Professor Horst Cerjak, 19.12.2005
5
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Synthia generates test data based on Design
by Contract specification
White
Box PEX, JPF
Design by
Contract Spec Explorer PEX, jTest
Black
Box
TGV, Overture
(VDM) ARTOO, JET
Test
sequence
Test
data
Synthia
Professor Horst Cerjak, 19.12.2005
6
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Design by Contract is a software concept
similar to legal contracts in real world
Client
Supplier pre
post
invariant
Idea from http://en.wikipedia.org/wiki/Design_by_contract
Professor Horst Cerjak, 19.12.2005
7
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Design by Contract is an incomplete
specification for classes and methods
Idea from http://en.wikipedia.org/wiki/Design_by_contract
Client
Supplier pre
post
invariant
Idea from http://en.wikipedia.org/wiki/Design_by_contract
Professor Horst Cerjak, 19.12.2005
8
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
A Fake Object is the most realistic
replacement for a real object
Dummy
• Passes bogus input values around
Stub
• Returns hard coded-values
Mock
• Focuses on Interaction
Spy
• Focuses on the objects state
Fake
• Swaps out real implementation
Google Testing Blog, „TotT: Friends you can depend on“, 12. Juni 2008
Professor Horst Cerjak, 19.12.2005
9
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
class Stack {
@Post(„size()==@Old(size())+1“)
public void push(int p1){...}
@Pure
@Post(„@Return>=0“)
public int size() {...}
}
Imagine we have to test a method that
requires at least two elements on a Stack
@Pre(„stack.size()>=2“)
@Post(„stack.size()==1“)
public void sumUp(Stack stack) {
int sum = 0;
while(stack.size()>0) {
sum += stack.pop();
}
stack.push(sum);
}
Professor Horst Cerjak, 19.12.2005
10
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
2,5 out of 1000 randomly generated Stack
objects have at least 2 elements
@Test
Public void testSumUp() {
Operator receiver = SumUpOp ();
Stack stack = new Stack();
stack.push(54);
stack.push(8432);
receiver.sumUp(stack);
}
Call method (50%)
Choose method that adds elements (10%)
Call another method (50%)
Choose method that adds elements (10%)
Professor Horst Cerjak, 19.12.2005
11
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Manually testing sumUp() would
feature mock objects to satisfy precondition
@Test
Public void testSumUp() {
Operator receiver = SumUpOp();
Stack stack =
new EasyMock.create(Stack);
expect(stack.size()).and Return(2);
replay(stack);
receiver.sumUp(stack);
}
Create Mock
Record expected behaviour
Start replaying behaviour
MUT is tested with recorded behaviour Stefan J. Galler, Andreas Maller, Franz Wotawa: „Automatically
Extracting Mock Object Behavior from Design by Contract
Specification for Test Data Generation“, AST 2010
Professor Horst Cerjak, 19.12.2005
12
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Static behaviour of mock
leads to false failing test cases
@Pre(„stack.size()>=2“)
@Post(„stack.size()==1“)
public void sumUp(Stack stack) {
int sum = 0;
while(stack.size()>0) {
sum += stack.pop();
}
stack.push(sum);
}
Precondition satisfied
while expression satisfied
Update sum
While expression satisfied
...
expect(stack.size()).and Return(2)
Professor Horst Cerjak, 19.12.2005
13
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Synthia synthesizes the behaviour of the
mock from Desing by Contract specification
@Test
Public void testSumUp() {
Operator receiver = SumUpOp();
Stack stack =
new Synthia<Stack>();
Map init = {size=2};
stack.setInitialState(init);
receiver.sumUp(stack);
}
Create Synthia Fake instance
Initial state calculated at test generation
Set initial values
Use fake object in MUT
Professor Horst Cerjak, 19.12.2005
14
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Synthia generates Fakes which replace
objects in unit tests
Stefan J. Galler, Andreas Maller, Franz Wotawa: „Automatically
Extracting Mock Object Behavior from Design by Contract
Specification for Test Data Generation“, AST 2010
Initial state calculation
Professor Horst Cerjak, 19.12.2005
15
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
A Synthia Fake behavior is determined by the
Design by Contract specification
CUT/MUT Parameter
Synthia Fake
jUnit Test set initial values
Professor Horst Cerjak, 19.12.2005
16
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
A Synthia Fake calculates the (public
observable) state change at runtime
Synthia
Fake
CUT/MUT SMT Solver
(yices, Z3)
size() := 2
...
size() := 1
...
pre post
size()
size() == @Old(size()) -1
Professor Horst Cerjak, 19.12.2005
17
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Synthia Fake handles incomplete
specification written in Java
Framing
• Unreferenced variables keep their values
Chaining
• Method calls within specification
Design-by-Contract keywords
• @Pure
• @Return
Professor Horst Cerjak, 19.12.2005
18
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Synthia Fake assumes that values are kept if
not stated otherwise
@Pure
@Post(„@Return>=0“)
public int size() {...}
@Pure
@Post(„@Return>=0 && size()==@Old(size())“)
public int size() {...}
Professor Horst Cerjak, 19.12.2005
19
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Chaining includes specification of referenced
methods
@Post(„size()==@Old(size())-1“)
public void pop(int p1){...}
@Pure
@Post(„@Return>=0“)
public int size() {...}
size()>=0 ;
size()==@Old(size())-1
; size()>=0
Professor Horst Cerjak, 19.12.2005
20
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Synthia uses the chained framed specification
for behavior calculation
@Post(„size()==@Old(size())-1“)
public void pop(int p1){...}
@Pure
@Post(„@Return>=0“)
public int size() {...}
size()>=0 && size()==@Old(size()) ;
size()==@Old(size())-1 ;
size()>=0 && size()==@Old(size())
Professor Horst Cerjak, 19.12.2005
21
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Synthia is evaluated on two case studies
stackcalc BillingSoftware
Professor Horst Cerjak, 19.12.2005
22
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
The stackcalc case study was developed and
later specified by students
• 868 NCSS
• 34 out of 147 methods
require non-primitive
data types as
parameters
Professor Horst Cerjak, 19.12.2005
23
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
The BillingSoftware is provided by our
industry partner
• 1456 NCSS
• 71 out of 249 methods
require non-primitive data
types as parameters
• First specified then
implemented
• Non-blocking queue had to
be replaced
Professor Horst Cerjak, 19.12.2005
24
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
We use a random approach (JET) to
benchmark Synthia
create
• Create input data for each method of all classes of the case study
classify
• Execute test online to classify the result
• Meaningless tests: precondition violation
export • Export all non-meaningless tests to jUnit
Professor Horst Cerjak, 19.12.2005
25
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Each data point is an average over 300
measurements
0%
20%
40%
60%
80%
100%
0 1 2 3 4 5 6 7 8 9 10
Union over 4 tries to
generate meaningful
test input data
function c
overa
ge
trials per run for each method under test
Professor Horst Cerjak, 19.12.2005
26
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
75%
80%
85%
90%
95%
100%
0 500 1000 1500 2000 2500
Random
Synthia
Synthia is able to test more methods
in less time
function c
overa
ge
generation time in ms
-30%
Professor Horst Cerjak, 19.12.2005
27
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
20%
30%
40%
50%
60%
70%
80%
90%
100%
0 1000 2000 3000 4000
Random (1) and (2)
Synthia (1)
Synthia (2)
function c
overa
ge
Strenghtening preconditions of constructors
boosted Synthia results
generation time in ms
+100%
Professor Horst Cerjak, 19.12.2005
28
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Test execution time depends on the
interaction of the test with Synthia Fakes
Test
execution
Random [sec] 0,24 0,53
Synthia [sec] 0,47 0,47
-11% to +95%
Professor Horst Cerjak, 19.12.2005
29
Stefan J. Galler SEFM 2010 - Synthia Competence Network Softnet Austria
Synthia tests need about 24%
more time for generation
Test
generation
Random [sec] 1,77 4,1
Synthia [sec] 2,12 5,11