Search-Based Software Engineers Need Tools Gordon Fraser, - - PowerPoint PPT Presentation

search based software engineers need tools
SMART_READER_LITE
LIVE PREVIEW

Search-Based Software Engineers Need Tools Gordon Fraser, - - PowerPoint PPT Presentation

Search-Based Software Engineers Need Tools Gordon Fraser, University of Sheffield Gordon Fraser, University of Sheffield, UK Andrea Arcuri, Simula Research Labs, Norway Contents 1. What is Search Based Software Testing? 2. Building an SBST


slide-1
SLIDE 1

Gordon Fraser, University of Sheffield, UK Andrea Arcuri, Simula Research Labs, Norway

Gordon Fraser, University of Sheffield

Search-Based Software Engineers Need Tools

slide-2
SLIDE 2

Contents

  • 1. What is Search Based Software Testing?
  • 2. Building an SBST Tool is Easy!
  • 3. The EvoSuite Test Generation Tool
  • 4. Lessons Learned Building an SBST Tool
slide-3
SLIDE 3

Source code Tests Automated test generation

slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6

Random Test Data Generation

Input

slide-7
SLIDE 7
slide-8
SLIDE 8
slide-9
SLIDE 9
slide-10
SLIDE 10

Generating vs Checking

Conventional Software Testing Research Write a method to construct test cases Search-Based Testing Write a method 
 to determine how good a test case is

slide-11
SLIDE 11

Generating vs Checking

Conventional Software Testing Research Write a method to construct test cases Search-Based Testing Write a fitness function
 to determine how good a test case is

slide-12
SLIDE 12

Fitness-guided search

Input Fitness

slide-13
SLIDE 13

Fitness-guided search

Input Fitness

slide-14
SLIDE 14

Search Operators

Components of an SBST Tool

Search Algorithm Representation Test Execution Instrumentation Fitness Function

def testMe(x, y): if x == 2 * (y + 1): return True else: return False

slide-15
SLIDE 15

Components of an SBST Tool

Meta-heuristic algorithm Measure how good a candidate solution is Execute tests

Search Algorithm Representation Search Operators Test Execution Instrumentation Fitness Function

Encoding of the problem solution Modifications of encoded solutions Collect data/traces for fitness calculation during execution

slide-16
SLIDE 16
slide-17
SLIDE 17
slide-18
SLIDE 18
slide-19
SLIDE 19

Contents

  • 1. What is Search Based Software Testing?
  • 2. Building an SBST Tool is Easy!
  • 3. The EvoSuite Test Generation Tool
  • 4. Lessons Learned Building an SBST Tool
slide-20
SLIDE 20

def testMe(x, y): if x == 2 * (y + 1): return True else: return False

slide-21
SLIDE 21

Components of an SBST Tool

Hill-climbing

Search Algorithm Representation Search Operators Test Execution Instrumentation Fitness Function

slide-22
SLIDE 22

(x, y) (x+1, y) (x-1, y) (x-1, y+1) (x, y+1) (x+1, y+1) (x-1, y-1) (x, y-1) (x+1, y-1)

def testMe(x, y): if x == 2 * (y + 1): return True else: return False

slide-23
SLIDE 23

Components of an SBST Tool

Hill-climbing

Search Algorithm Representation Search Operators Test Execution Instrumentation Fitness Function

Tuple (x, y) Neighbourhood of (x, y)

slide-24
SLIDE 24

Hill Climbing

  • 1. Select Random

Value

slide-25
SLIDE 25

Hill Climbing

  • 2. Explore

Neighbourhood

slide-26
SLIDE 26

Hill Climbing

  • 3. Choose better

neighbour

slide-27
SLIDE 27

Hill Climbing

  • 4. Repeat until
  • ptimum is found
slide-28
SLIDE 28

Components of an SBST Tool

Hill-climbing

Search Algorithm Representation Search Operators Test Execution Instrumentation Fitness Function

Tuple (x, y) Neighbourhood of (x, y)

slide-29
SLIDE 29

SUT Input Output

slide-30
SLIDE 30

SUT Instrumented SUT Input Output Trace }

Fitness Test Data

slide-31
SLIDE 31

def testMe(x, y): if x == 2 * (y + 1): return True else: return False

slide-32
SLIDE 32

Components of an SBST Tool

Branch distance Call method

Search Algorithm Representation Search Operators Test Execution Instrumentation Fitness Function

Global variable Hill-climbing Tuple (x, y) Neighbourhood of (x, y)

slide-33
SLIDE 33

10 1 2 3 4 5 6 7 8 9 9 1 2 3 4 5 6 7 8

Input Value

Fitness

slide-34
SLIDE 34

9 1 2 3 4 5 6 7 8

Input Value

Fitness

  • (231)

231-1

slide-35
SLIDE 35

def testMe(x, y): if x == 2 * y and y > 1: return True else: return False

slide-36
SLIDE 36

Branch Distance

Distance True Distance False |x - y| 1 1 |x - y| y - x + 1 x - y y - x x - y + 1 x - y+ 1 x - y x - y x - y + 1 Expression x == y x != y x > y x >= y x < y x <= y

slide-37
SLIDE 37

def testMe(x, y): if x == 2 * y and y > 1: return True else: return False

slide-38
SLIDE 38

def testMe(x, y): if x <= y: if x == y: print("Some output") if x > 0: if y == 17: # Target Branch return True return False

slide-39
SLIDE 39

Entry Exit x <= y x == y x > 0 y == 17 return False return True print

true true true true false false false false

def testMe(x, y): if x <= y: if x == y: print("Some output") if x > 0: if y == 17: # Target Branch return True return False

slide-40
SLIDE 40

Entry Exit x <= y x == y x > 0 y == 17 return False return True print

true true true true false false false false

Entry Exit x <= y x == y x > 0 y == 17 return False return True print

true true false true true true

slide-41
SLIDE 41

Covering a structure

TARGET

slide-42
SLIDE 42

Fitness evaluation

TARGET

The test data executes the ‘wrong’ path

slide-43
SLIDE 43

Approach Level

TARGET

= 2 = 1 = 0 minimisation

slide-44
SLIDE 44

Putting it all together

true true

if a >= b if b >= c

TARGET TARGET MISSED
 Approach Level = 1
 Branch Distance = c - b TARGET MISSED
 Approach Level = 2
 Branch Distance = b - a

false false true

if c >= d

false

TARGET MISSED
 Approach Level = 0
 Branch Distance = d - c

Fitness = approach Level + normalised branch distance

TARGET

normalised branch distance between 0 and 1
 indicates how close approach level is to being penetrated

slide-45
SLIDE 45

9 1 2 3 4 5 6 7 8

Input Value

Fitness

  • (231)

231-1

slide-46
SLIDE 46

9 1 2 3 4 5 6 7 8

Input Value

Fitness

  • (231)

231-1

slide-47
SLIDE 47

9 1 2 3 4 5 6 7 8

Input Value

Fitness

  • (231)

231-1

slide-48
SLIDE 48

Evolutionary Testing

Mutation Crossover Selection Insertion Fitness Evaluation End?

Test cases Monitoring Execution

slide-49
SLIDE 49
slide-50
SLIDE 50

Crossover

a b c

10 10 20 40

d a b c

20

  • 5

80 80

d c

80 80

d a

20

b

  • 5

a

10

b

10

c

20 40

d

slide-51
SLIDE 51

d

40

Mutation

a b c

10 10 20 20

d

40

d a

20

slide-52
SLIDE 52
  • Selective pressure:


The higher, the more likely the fittest are chosen

  • Stagnation:


Selective pressure too small

  • Premature convergence:


Selective pressure too high

  • Standard algorithms:


Rank selection, tournament selection, roulette wheel selection

Selection

slide-53
SLIDE 53

Contents

  • 1. What is Search Based Software Testing?
  • 2. Building an SBST Tool is Easy!
  • 3. The EvoSuite Test Generation Tool
  • 4. Lessons Learned Building an SBST Tool
slide-54
SLIDE 54

@Test public void test() { } int x = 2; int y = 2; int result = x + y; assertEquals(4, result);

slide-55
SLIDE 55

@Test public void test() { }

DateTime var3 = var1.toDateTime(var2); DateTime var4 = var3.minus(var0); TimeOfDay var2 = new TimeOfDay(); YearMonthDay var1 = new YearMonthDay(var0); int var0 = 10 DateTime var5 = var4.plusSeconds(var0);

slide-56
SLIDE 56

Test Suite Generation

Initialize Population Select parents Recombine parents Return best solution While not done

DateTime var3 = var1.toDateTime(var2); DateTime var4 = var3.minus(var0); TimeOfDay var2 = new TimeOfDay(); YearMonthDay var1 = new YearMonthDay(var0); int var0 = 10 DateTime var5 = var4.plusSeconds(var0);
slide-57
SLIDE 57

Test Suite Generation

slide-58
SLIDE 58

Crossover

slide-59
SLIDE 59

Mutation

slide-60
SLIDE 60

Mutation

slide-61
SLIDE 61

Fitness

public int gcd(int x, int y) { int tmp; while (y != 0) { tmp = x % y; x = y; y = tmp; } return x; }

slide-62
SLIDE 62

Components of an SBST Tool

Sum of branch distances (and others) Java reflection

Search Algorithm Representation Search Operators Test Execution Instrumentation Fitness Function

Java bytecode instrumentation Genetic Algorithm (+Archive, Seeding, Local Search, DSE) Sets of sequences of Java statements Standard GA operators implemented for test suites

slide-63
SLIDE 63

EvoSuite

http://www.evosuite.org/downloads

  • Jar release - for command line usage
  • Maven plugin
  • IntelliJ plugin
  • Eclipse plugin
  • Jenkins plugin
slide-64
SLIDE 64

Does it work?

Bugs found

0% 25% 50% 75% 100%

JFreeChart Closure Math Lang Joda Time

0.1 0.2 0.3 0.4

10% 20% 30% 40% 50% 60% 70% 80% 90% 100%

Defects4J: 357 real bugs SF110: 23,886 Classes
 6,628,619 LOC

  • G. Fraser, A. Arcuri. “A Large Scale Evaluation of Automated

Unit Test Generation with EvoSuite” TOSEM 24(2), 2014. Shamshiri et al. "Do Automatically Generated Unit Tests Find Real Faults? An Empirical Study of Effectiveness and Challenges” ASE, 2015

Point is: It takes a tool and lots of engineering to do this.

slide-65
SLIDE 65

Coverage

25 50 75 100 Option Rational DocType ArrayIntList

EvoSuite Manual

slide-66
SLIDE 66

Time Spent on Testing

6.5 13 19.5 26 FilterIterator FixedOrderComparator ListPopulation PredicatedMap

Assisted Manual

slide-67
SLIDE 67

Fault Detection

0.5 1 1.5 2 Option Rational DocType ArrayIntList

EvoSuite Manual

slide-68
SLIDE 68

Faults Prevention

4 8 12 16 FilterIterator FixedOrderComparator ListPopulation PredicatedMap

Assisted Manual

slide-69
SLIDE 69

Method Names

@Test(timeout = 4000) public void testFooReturningFalse() throws Throwable { StringExample stringExample0 = new StringExample(); boolean boolean0 = stringExample0.foo(""); assertFalse(boolean0); } @Test(timeout = 4000) public void test3() throws Throwable { StringExample stringExample0 = new StringExample(); boolean boolean0 = stringExample0.foo(""); assertFalse(boolean0); }

slide-70
SLIDE 70

Variable Names

@Test(timeout = 4000) public void testFooReturningFalse() throws Throwable { StringExample stringExample0 = new StringExample(); boolean boolean0 = stringExample0.foo(""); assertFalse(boolean0); } @Test(timeout = 4000) public void testFooReturningFalse() throws Throwable { StringExample invokesFoo = new StringExample(); boolean resultFromFoo = invokesFoo.foo(""); assertFalse(resultFromFoo); }

slide-71
SLIDE 71

Variable Names

public class Foo { public void foo() { StringExample sx = new StringExample(); boolean bar = sx.foo(""); } } @Test(timeout = 4000) public void testFooReturningFalse() throws Throwable { StringExample sx = new StringExample(); boolean bar = sx.foo(""); assertFalse(bar); }

slide-72
SLIDE 72
slide-73
SLIDE 73

Readability Model

slide-74
SLIDE 74

Time Spent Understanding

Time (min) 1.75 3.5 5.25 7

S t d X M L R e a d e r A t t r i b u t e C h a i n B a s e O p t i

  • n

F i x e d O r d e r C

  • m

p a r a t

  • r

F i l t e r L i s t I t e r a t

  • r

P l u g i n R u l e s R u l e s B a s e C h a r R a n g e Y e a r M

  • n

t h D a y

Default Optimised

slide-75
SLIDE 75

Contents

  • 1. What is Search Based Software Testing?
  • 2. Building an SBST Tool is Easy!
  • 3. The EvoSuite Test Generation Tool
  • 4. Lessons Learned Building an SBST Tool
slide-76
SLIDE 76
  • 1. Java

…is a weird language and never ceases to surprise me My personal enemy: Java Generics Bytecode over sourcecode - yes!

slide-77
SLIDE 77
  • 2. Corner Cases

The more corner cases you cover ...the more can go wrong ...the more new corner cases you will find ...the slower EvoSuite becomes

slide-78
SLIDE 78
  • 2. Corner Cases
  • Constant Seeding: +5%
  • Virtual FS: +1.4%
  • Mocking +4.7%
  • JEE support: +3%
  • DSE: +1.2%
slide-79
SLIDE 79
  • 3. Developers

…some really care only about coverage …others don’t care about coverage:


"I wouldn’t normally in real life be aiming for 100% coverage. I’d probably end up with fewer tests without this tool but I couldn’t tell you if they would be all the right tests.”

…do not want their tests to be generated …hate ugly tests …don’t like waiting Talk to them!

slide-80
SLIDE 80
  • 3. Developers

public class Example { private Example() {} // … }

slide-81
SLIDE 81
  • 4. Testing

Testing randomised algorithms is difficult Make the implementation deterministic Always use LinkedHashSet over HashSet, LinkedHashMap over HashMap Java reflection is not deterministic Avoid static state (e.g. singletons)

slide-82
SLIDE 82
  • 4. Testing

EvoSuite uses one central random number generator Any change will affect something at a completely different part of the program Change seeds frequently during testing to find flaky tests

slide-83
SLIDE 83
  • 5. Documentation

I don’t comment my code Students struggle I spend more time explaining things than it would take me to implement them

slide-84
SLIDE 84
  • 6. Tool Comparisons

Reviewers want to see them I don’t like doing them It’s impossible to make them fair Contact tool authors Report bugs Make your own tools usable

slide-85
SLIDE 85
  • 7. Open Source

“The source code will be released under an open source library (most likely GPL2) at a later point, as soon as a number of refactorings are completed.” — FSE’11 tool paper appendix Public GitHub repo: 2015 It will never be clean enough, just release it!

slide-86
SLIDE 86
  • 8. Licensing

License matters Google will not touch GPL BSD, MIT - do you want others to become rich with your idea? Gnu Lesser Public License, Apache

slide-87
SLIDE 87
  • 9. Tool Papers

The first one will be cited The rest no one will cite It shouldn’t be this way

slide-88
SLIDE 88
  • 10. Tool Building

Building a quick prototype is easy Building a real tool is difficult …and will give you a paper …but lets you identify many new problems …lets you talk to developers …lets other people build on your work …will give you lots of citations and papers

slide-89
SLIDE 89
  • 10. Tool Building

Building a quick prototype is easy Building a real tool is difficult …and will give you a paper …but lets you identify many new problems …lets you talk to developers …lets other people build on your work …will give you lots of citations and papers

Search-Based Software Engineers Need Tools!

slide-90
SLIDE 90

Gordon Fraser, University of Sheffield, UK Andrea Arcuri, Simula Research Labs, Norway

www.evosuite.org