Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Property Based Tesing

73 views

Published on

Property Based Testing applied properly can improve unit test quality, productivity and coverage.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Property Based Tesing

  1. 1. Property Based Testing Mårten Rånge Atomize - Automatic revenue management
  2. 2. A simple idea...
  3. 3. Create a test for a property of our code
  4. 4. Let the computer generate test data
  5. 5. If the property held for all data the test passed
  6. 6. Property Based Testing done properly... ▸Improves productivity ▸Improves test quality ▸Finds more bugs during development
  7. 7. Map<'K, 'V>
  8. 8. §1 – The map is immutable §2 – Lookup after set shall succeed §3 – Lookup after unset shall fail
  9. 9. Red Tree
  10. 10. 3 1 ~ ~ 5 ~ 7 ~ ~
  11. 11. §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 contains the same number of nodes §7 – §5 + §6  Tree depth at most 2[log2 (n + 1)]
  12. 12. 3 1 ~ ~ 5 ~ 7 ~ ~
  13. 13. type Color = | Red | Black type Tree<'K, 'V> = | Leaf // Left child Right child | Node of Color*Tree<'K, 'V>*'K*'V*Tree<'K, 'V>
  14. 14. val set: 'K -> 'V -> Tree<'K, 'V> -> Tree<'K, 'V> val lookup: 'K -> Tree<'K, 'V> -> 'V option
  15. 15. Demo
  16. 16. Common patterns
  17. 17. Oracles
  18. 18. static member ``test chunkBySize`` (sz : int) (vs : int []) = let sz = wrap sz - 1 vs.Length let e = vs |> chunkBySize sz let a = vs |> Stream.ofArray |> Stream.chunkBySize sz |> Stream.toArray e = a static member ``test collect`` (vs : int [] []) = let e = vs |> Array.collect id let a = vs |> Stream.ofArray |> Stream.collect Stream.ofArray |> Stream.toArray e = a
  19. 19. Actions
  20. 20. type HeapActions = | Push of int*int64 | Pop static member ``test`` (a : HeapActions []) = ...
  21. 21. Test Models
  22. 22. type Json = | JsonNull | JsonBoolean of bool | JsonNumber of float | JsonString of string | JsonArray of Json [] | JsonObject of (string*Json) []
  23. 23. 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) []
  24. 24. Randomized testing?
  25. 25. Pseudo randomized testing
  26. 26. The seed is traced  Reproducible
  27. 27. Specify the seed  Predictable
  28. 28. Property Based Testing Frameworks ▸QuickCheck (Haskell) ▸FsCheck (.NET) ▸ScalaCheck (Scala) ▸JUnit-QuickCheck (Java)
  29. 29. Questions? marten@atomize.com

×