Top Banner
Property Based Testing Mårten Rånge Atomize - Automatic revenue management
29

Property Based Tesing

Jan 28, 2018

Download

Software

Mårten Rånge
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Property Based Tesing

Property Based Testing

Mårten Rånge

Atomize - Automatic revenue management

Page 2: Property Based Tesing

A simple idea...

Page 3: Property Based Tesing

Create a test for a

property of our code

Page 4: Property Based Tesing

Let the computer

generate test data

Page 5: Property Based Tesing

If the property held for

all data the test passed

Page 6: Property Based Tesing

Property Based Testing done properly...

▸Improves productivity

▸Improves test quality

▸Finds more bugs during development

Page 7: Property Based Tesing

Map<'K, 'V>

Page 8: Property Based Tesing

§1 – The map is immutable

§2 – Lookup after set shall succeed

§3 – Lookup after unset shall fail

Page 9: Property Based Tesing

Red Tree

Page 10: Property Based Tesing

3

1

~ ~

5

~ 7

~ ~

Page 11: Property Based Tesing

§4 – Parent key is greater than left child,smaller than right child

§5 – No Red Node has a Red child

§6 – Every path from the Root to a Leaf containsthe same number of nodes

§7 – §5 + §6 Tree depth at most 2[log2 (n + 1)]

Page 12: Property Based Tesing

3

1

~ ~

5

~ 7

~ ~

Page 13: Property Based Tesing

type Color =| Red| Black

type Tree<'K, 'V> =| Leaf // Left child Right child| Node of Color*Tree<'K, 'V>*'K*'V*Tree<'K, 'V>

Page 14: Property Based Tesing

val set: 'K -> 'V -> Tree<'K, 'V> -> Tree<'K, 'V>

val lookup: 'K -> Tree<'K, 'V> -> 'V option

Page 15: Property Based Tesing

Demo

Page 16: Property Based Tesing

Common patterns

Page 17: Property Based Tesing

Oracles

Page 18: Property Based Tesing

static member ``test chunkBySize`` (sz : int) (vs : int []) =let sz = wrap sz - 1 vs.Lengthlet e = vs |> chunkBySize szlet a = vs |> Stream.ofArray

|> Stream.chunkBySize sz |> Stream.toArray

e = a

static member ``test collect`` (vs : int [] []) =let e = vs |> Array.collect idlet a = vs |> Stream.ofArray

|> Stream.collect Stream.ofArray |> Stream.toArray

e = a

Page 19: Property Based Tesing

Actions

Page 20: Property Based Tesing

type HeapActions =| Push of int*int64| Pop

static member ``test`` (a : HeapActions []) =...

Page 21: Property Based Tesing

Test Models

Page 22: Property Based Tesing

type Json =| JsonNull| JsonBoolean of bool| JsonNumber of float| JsonString of string| JsonArray of Json []| JsonObject of (string*Json) []

Page 23: Property Based Tesing

type Ws = Whitespace []* Whitespace []

type WsJson =| WsJsonNull Ws| WsJsonBoolean of Ws*bool| WsJsonNumber of Ws*float| WsJsonString of Ws*string| WsJsonArray of Ws*WsJson []| WsJsonObject of Ws*((Ws*string)*Json) []

Page 24: Property Based Tesing

Randomized testing?

Page 25: Property Based Tesing

Pseudo randomized testing

Page 26: Property Based Tesing

The seed is traced

Reproducible

Page 27: Property Based Tesing

Specify the seed

Predictable

Page 28: Property Based Tesing

Property Based Testing Frameworks

▸QuickCheck (Haskell)

▸FsCheck (.NET)

▸ScalaCheck (Scala)

▸JUnit-QuickCheck (Java)

Page 29: Property Based Tesing

Questions?

[email protected]