Slide 1

Slide 1 text

@yot88 PROPERTY-BASED TESTING (PBT) PBT

Slide 2

Slide 2 text

@yot88 A DEV STORY Scott Wlaschin https://fr.slideshare.net/ScottWlaschin/an-introduction-to-property-based-testing

Slide 3

Slide 3 text

A dev story

Slide 4

Slide 4 text

A dev story

Slide 5

Slide 5 text

A dev story

Slide 6

Slide 6 text

A dev story

Slide 7

Slide 7 text

@yot88 Which tests would you have been written ?

Slide 8

Slide 8 text

A dev story

Slide 9

Slide 9 text

A dev story

Slide 10

Slide 10 text

A dev story

Slide 11

Slide 11 text

A dev story

Slide 12

Slide 12 text

A dev story

Slide 13

Slide 13 text

A dev story

Slide 14

Slide 14 text

A dev story

Slide 15

Slide 15 text

A dev story What is our code coverage ?

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

@yot88 Let’s add new tests

Slide 18

Slide 18 text

@yot88 A dev story WTF ?

Slide 19

Slide 19 text

@yot88 A dev story

Slide 20

Slide 20 text

A dev story

Slide 21

Slide 21 text

@yot88 A dev story

Slide 22

Slide 22 text

@yot88 A dev story Let’s rethink the approach Do not use specific examples anymore

Slide 23

Slide 23 text

@yot88 A dev story We can’t test the add by using +

Slide 24

Slide 24 text

@yot88 PBT to the rescue

Slide 25

Slide 25 text

@yot88 Testing with requirements / properties What about parameter order ? (compared to subtract) The parameter order does not matter when_I_add_two_numbers_the_result_should_not_depend_on_parameter_order “add 1” twice is the same as doing “add 2” (compared to multiply) What about adding 0 ? Adding 0 does nothing We are confident the implementation is correct if we test these properties It applies to all inputs

Slide 26

Slide 26 text

Specification with properties Associativity property Commutativity property Identity property

Slide 27

Slide 27 text

@yot88 Is there a simpler way to do it ?

Slide 28

Slide 28 text

@yot88 What is Property based testing ? A property is the combination of an invariant with an input values generator. For each generated value, the invariant is treated as a predicate and checked whether it yields true or false for that value. As soon as there is one value which yields false, the property is said to be falsified, and checking is aborted. If a property cannot be invalidated after a specific amount of sample data, the property is assumed to be satisfied.

Slide 29

Slide 29 text

What is Property based testing PBT

Slide 30

Slide 30 text

@yot88 What is Property based testing ? Describe the input Describe the properties of the output Have the computer try lots of random examples Check if it fails

Slide 31

Slide 31 text

@yot88 What is Property based testing

Slide 32

Slide 32 text

@yot88 What can we do in java ?

Slide 33

Slide 33 text

Junit-quickcheck https://pholser.github.io/junit-quickcheck/site/0.9.1/

Slide 34

Slide 34 text

Junit-quickcheck

Slide 35

Slide 35 text

Junit-quickcheck

Slide 36

Slide 36 text

Junit-quickcheck

Slide 37

Slide 37 text

Junit-quickcheck

Slide 38

Slide 38 text

Junit-quickcheck

Slide 39

Slide 39 text

Junit-quickcheck

Slide 40

Slide 40 text

Junit-quickcheck

Slide 41

Slide 41 text

Junit-quickcheck

Slide 42

Slide 42 text

Junit-quickcheck

Slide 43

Slide 43 text

@yot88 And in real life ?

Slide 44

Slide 44 text

Real life

Slide 45

Slide 45 text

Real life – identify properties The balance should be decremented of the withdrawal amount when I have enough money Or the overdraft is authorized for my account I must not be allowed to withdraw when The withdraw amount is over my max withdrawal amount My balance is insufficient, and overdraft is not authorized for my account

Slide 46

Slide 46 text

Real life – custom generators To use them Or use the serviceloader by creating a file called: com.pholser.junit.quickcheck.generator.Generator in META-INF/services

Slide 47

Slide 47 text

Real life – implementation

Slide 48

Slide 48 text

@yot88 Vavr stuff ?

Slide 49

Slide 49 text

PBT in vavr

Slide 50

Slide 50 text

PBT in vavr

Slide 51

Slide 51 text

@yot88 And so ?

Slide 52

Slide 52 text

@yot88 PBT and example-based tests PBTs are more general 1 property-based test can replace many example-based tests PBTs can reveal edge cases Nulls, negative numbers, weird strings (encoding for example), … PBTs ensures deep understanding of the business invariants Example-based tests are still helpful though Easier to understand & start with

Slide 53

Slide 53 text

@yot88 What could we do together ? NEXT STEPS

Slide 54

Slide 54 text

@yot88 Resources An introduction to property-based testing – Scott Wlaschin Property-Based Testing for everyone - Romeu Moura (Video) Property-based testing in Java with JUnit-Quickcheck - Kenny Baas-Schwegler https://baasie.com/2017/06/12/property-based-testing-in-java-with-junit-quickcheck-part-2- generators/ Property Testing with vavr junit-quickcheck documentation