property-based testing in java - erasmus mundus · introduction propcheck: a testing framework...

30
Introduction PropCheck: A testing framework Factory Generator Conclusion Property-based testing in Java Ahmad Abdelghany Supervisors: Horatiu Cirstea Pierre-Etienne Moreau June, 2015 Property-based testing in Java 1/24 Ahmad Abdelghany(UL)

Upload: others

Post on 25-May-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Property-based testing in Java

Ahmad Abdelghany

Supervisors:

Horatiu Cirstea Pierre-Etienne Moreau

June, 2015

Property-based testing in Java 1/24 Ahmad Abdelghany(UL)

Page 2: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Software Quality

Ensure correctness, security, reliability, . . .Most importantly, bug-free software.

Cost of fixing software bugs

Property-based testing in Java 2/24 Ahmad Abdelghany(UL)

Page 3: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Software Quality

Ensure correctness, security, reliability, . . .Most importantly, bug-free software.Cost of fixing software bugs

Property-based testing in Java 2/24 Ahmad Abdelghany(UL)

Page 4: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Property-based testing

General IdeaInstead of one-input test scenario, a function is tested withmany auto-generated test inputs.Properties that a function should fulfill are verified.No external specification language/tools needed.Higher confidence in quality.

Property-based testing in Java 3/24 Ahmad Abdelghany(UL)

Page 5: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Outline

1 Introduction

2 PropCheck: A testing framework

3 Factory Generator

4 Conclusion

Property-based testing in Java 4/24 Ahmad Abdelghany(UL)

Page 6: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Outline

1 IntroductionWhat is property-based testing?

2 PropCheck: A testing frameworkImplementationPropCheck in action

3 Factory GeneratorImplementation

4 Conclusion

Property-based testing in Java 5/24 Ahmad Abdelghany(UL)

Page 7: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

What is property-based testing?

Property-based testing tools

In Haskell:QuickCheck [ICFP00] - Random testingSmallCheck [ACM08] - Exhaustive testing

In Java:QuickCheck for Java, JCheck, junit-quickcheck⇒ Only primitives and some built-in types (e.g. arrays, Lists)⇒ No support for user-defined data types.⇒ Eager by nature.

Property-based testing in Java 6/24 Ahmad Abdelghany(UL)

Page 8: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

What is property-based testing?

Verifying Properties

∀s : Stack ,n ∈ N • top(push(s,n)) = n

@DataPoints int[] n = {1, -3, 5}@DataPointsStack[] stack = {getListOfStacks()}@Theorypublic void testTop(Stack s, int n) {

assertThat(s.push(n).top(), is(n));}

Property-based testing in Java 7/24 Ahmad Abdelghany(UL)

Page 9: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

What is property-based testing?

Verifying Properties

∀s : Stack ,n ∈ N • top(push(s,n)) = n

@Theorypublic void testTop(

@ForSome Stack s, @ForSome int n) {

assertThat(s.push(n).top(), is(n));}

Property-based testing in Java 7/24 Ahmad Abdelghany(UL)

Page 10: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

What is property-based testing?

Verifying Properties

∀s : Stack ,n ∈ N • top(push(s,n)) = n

@Theorypublic void testTop(

@ForSome Stack s, @ForSome int n) {

assertThat(s.push(n).top(), is(n));}

Testing testTop().100 tests were generated.(tested/assumption violation/bad input):

100/0/0

Property-based testing in Java 7/24 Ahmad Abdelghany(UL)

Page 11: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

What is property-based testing?

Contribution

Design & Implementation of Factory generator.The factory is part of PropCheck testing framework.Enumerate user-defined Java types.Successfully tested for different use cases.

Property-based testing in Java 8/24 Ahmad Abdelghany(UL)

Page 12: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Outline

1 IntroductionWhat is property-based testing?

2 PropCheck: A testing frameworkImplementationPropCheck in action

3 Factory GeneratorImplementation

4 Conclusion

Property-based testing in Java 9/24 Ahmad Abdelghany(UL)

Page 13: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Implementation

Architecture

Property-based testing in Java 10/24 Ahmad Abdelghany(UL)

Page 14: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

PropCheck in action

JavaFeat enumerator

FEAT Functional Enumeration of Algebraic Types.[ACM12]Enumeration Lazy (possibly infinite) set of Terms of type T

Parts Finite subsets containing terms of given sizeSize Term size is computed as number of constructors

Property-based testing in Java 11/24 Ahmad Abdelghany(UL)

Page 15: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

PropCheck in action

Algebraic data types

Bool = False | TrueEnumeration<Boolean> boolEnum =

Enumeration.singleton(true).plus(Enumeration.singleton(false)

))

Character = a | b | c . . . x | y | zEnumeration<Character> charEnum =

Enumeration.singleton(a).plus(Enumeration.singleton(b) ...

))

Nat = Zero | Succ Nat

Property-based testing in Java 12/24 Ahmad Abdelghany(UL)

Page 16: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

PropCheck in action

Algebraic data types

Bool = False | TrueEnumeration<Boolean> boolEnum =

Enumeration.singleton(true).plus(Enumeration.singleton(false)

))

Character = a | b | c . . . x | y | zEnumeration<Character> charEnum =

Enumeration.singleton(a).plus(Enumeration.singleton(b) ...

))

Nat = Zero | Succ Nat

Property-based testing in Java 12/24 Ahmad Abdelghany(UL)

Page 17: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

PropCheck in action

Enumerating Java types as algebraic types

public class Student {...public Student(int code,String name) {

this.code = code;this.name = string;

}}

Student = student(i:Int, s:String) | ...

Our options are:Use Tom/Gom extensions.Manual definition.

automate?!

Property-based testing in Java 13/24 Ahmad Abdelghany(UL)

Page 18: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

PropCheck in action

Enumerating Java types as algebraic types

public class Student {...public Student(int code,String name) {

this.code = code;this.name = string;

}}

Student = student(i:Int, s:String) | ...

Our options are:Use Tom/Gom extensions.Manual definition.

automate?!

Property-based testing in Java 13/24 Ahmad Abdelghany(UL)

Page 19: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

PropCheck in action

Enumerating Java types as algebraic types

public class Student {...public Student(int code,String name) {

this.code = code;this.name = string;

}}

Student = student(i:Int, s:String) | ...

Our options are:Use Tom/Gom extensions.Manual definition.

automate?!

Property-based testing in Java 13/24 Ahmad Abdelghany(UL)

Page 20: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Outline

1 IntroductionWhat is property-based testing?

2 PropCheck: A testing frameworkImplementationPropCheck in action

3 Factory GeneratorImplementation

4 Conclusion

Property-based testing in Java 14/24 Ahmad Abdelghany(UL)

Page 21: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Implementation

Factory generator overview

Uses Java Reflection API.Captures type definition via annotations.Uses Apache Velocity template engine.Generates custom factories.Works for many structurally-complex types.

Property-based testing in Java 15/24 Ahmad Abdelghany(UL)

Page 22: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Implementation

Simple types

@Enumeratepublic Student(

@Enumerate(maxSize = 8) int code,@Enumerate(maxSize = 4) String name

) {this.code = code;this.name = name;

}

public class StudentFactory {public static final Enumeration<Student>

getEnumeration() {...

}}

Property-based testing in Java 16/24 Ahmad Abdelghany(UL)

Page 23: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Implementation

Generated Factory

public static final Enumeration<Student>getEnumeration() {

...F<Integer, F<String, Student>> _student =...Enumeration<Integer> noEnum =

Combinators.makeInteger().parts().take(8);...Enumeration<String> stringEnum =

Combinators.makeString().parts().take(4));...

}

Property-based testing in Java 17/24 Ahmad Abdelghany(UL)

Page 24: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Implementation

Handling Dependencies

One-To-One relations:Factory for referenced type generated.Generated factory used in referencing type.

One-To-Many relations:Factory for referenced type generated.Collections factories used.

Many-To-Many relations:Decomposed into One-To-Many relations.

Property-based testing in Java 18/24 Ahmad Abdelghany(UL)

Page 25: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Implementation

Abstract types

No constructors!

@Enumeratepublic Person(

@Enumerate(concreteClasses = {Dog.class, Cat.class}

) Pet pet) {

this.pet = pet;}

Size --> Enumeration0 --> []1 --> [Cat [name=, age=0, nick=], Dog [name=, age=0]]2 --> [Cat[name=, age=0, nick=a],...,Dog[name=a, age=0],...]

Property-based testing in Java 19/24 Ahmad Abdelghany(UL)

Page 26: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Implementation

Mutually-recursive types

Challenges:1 Detecting cyclic types.2 Reference To

Enumeration.Solution:

1 Build dependency tree.2 Define a fix point.

Property-based testing in Java 20/24 Ahmad Abdelghany(UL)

Page 27: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Implementation

Construction from methods

@Enumeratepublic ListStack() {

stack = new ArrayList<Integer>();}@Enumeratepublic IStack push(@Enumerate Integer elem) {

stack.add(elem);return this;

}

We need a reference to "this" instance to call non-staticmethodsHandled as recursive types.Generated enumeration is used to refer to "this" instance.

Property-based testing in Java 21/24 Ahmad Abdelghany(UL)

Page 28: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Outline

1 IntroductionWhat is property-based testing?

2 PropCheck: A testing frameworkImplementationPropCheck in action

3 Factory GeneratorImplementation

4 Conclusion

Property-based testing in Java 22/24 Ahmad Abdelghany(UL)

Page 29: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Conclusion and future work

Conclusion:Powerful property-based testing framework for Java.Functional enumeration of built-in and user-defined types(lazily).Easy to use and available in all Java environments.

Future work:Consider more complex data types.Specify distribution of test data.

Property-based testing in Java 23/24 Ahmad Abdelghany(UL)

Page 30: Property-based testing in Java - Erasmus Mundus · Introduction PropCheck: A testing framework Factory GeneratorConclusion What is property-based testing? Property-based testing tools

Introduction PropCheck: A testing framework Factory Generator Conclusion

Thank You!

Property-based testing in Java 24/24 Ahmad Abdelghany(UL)