Quickcheck - Random Property-Based Testing
Total Page:16
File Type:pdf, Size:1020Kb
QuickCheck - Random Property-based Testing Arnold Schwaighofer QuickCheck - Random Property-based Haskell - A Truly (Cool) Functional Programming Testing Language Why Functional Programming And In Particular Quickcheck - A Truly Cool QuickCheck Is Cool Property Based Testing Tool Summary Custom Random Arnold Schwaighofer Data Generators Resources Institute for Formal Models and Verification References Johannes Kepler University Linz 26 June 2007 / KV Debugging QuickCheck - Random Outline Property-based Testing Arnold Schwaighofer Haskell - A Truly (Cool) Functional Programming Language Haskell - A Truly (Cool) Functional What - Functional Programming Programming Language How - Functional Programming Quickcheck - A Why - Functional Programming Truly Cool Property Based Testing Tool Quickcheck - A Truly Cool Property Based Testing Tool Summary Custom Random What - Random Property Based Testing Data Generators How - Random Property Based Testing Resources Why - Random Property Based Testing References Success Stories Related Work And Outlook Summary QuickCheck - Random Outline Property-based Testing Arnold Schwaighofer Haskell - A Truly (Cool) Functional Programming Language Haskell - A Truly (Cool) Functional What - Functional Programming Programming Language How - Functional Programming Quickcheck - A Why - Functional Programming Truly Cool Property Based Testing Tool Quickcheck - A Truly Cool Property Based Testing Tool Summary Custom Random What - Random Property Based Testing Data Generators How - Random Property Based Testing Resources Why - Random Property Based Testing References Success Stories Related Work And Outlook Summary QuickCheck - Random Outline Property-based Testing Arnold Schwaighofer Haskell - A Truly (Cool) Functional Programming Language Haskell - A Truly (Cool) Functional What - Functional Programming Programming Language How - Functional Programming Quickcheck - A Why - Functional Programming Truly Cool Property Based Testing Tool Quickcheck - A Truly Cool Property Based Testing Tool Summary Custom Random What - Random Property Based Testing Data Generators How - Random Property Based Testing Resources Why - Random Property Based Testing References Success Stories Related Work And Outlook Summary QuickCheck - Random What Is Functional Programming? Property-based Testing From a lazy perspective Arnold Schwaighofer Haskell - A Truly As in features of the programming language Haskell (Cool) Functional Programming [JH99]. Language What - Functional Functions are first class - values that can be passed Programming I How - Functional Programming around Why - Functional Programming I Referential integrity - no side effects! Quickcheck - A Truly Cool I Pattern matching - Write functions according to the Property Based Testing Tool type’s data constructor Summary I Laziness - evaluate terms when they are needed Custom Random Data Generators (and only once) Resources I Statically typed - all terms must have a valid type at References compile time QuickCheck - Random Functions As Essential Building Blocks Property-based Testing Functions are curried Arnold Schwaighofer multiply :: Integer → Integer → Integer Haskell - A Truly multiply x y = x ∗ y (Cool) Functional Programming Language Functions build of other functions What - Functional Programming How - Functional Programming multiplyByTwo :: Integer → Integer Why - Functional Programming multiplyByTwo x = multiply 2 x Quickcheck - A Truly Cool Property Based Functions can be polymorphic Testing Tool Summary id :: a → a Custom Random Data Generators id x = x Resources References applyTwice :: (a → a) → a → a applyTwice f x = f (f x) file:///Users/arnold/Desktop/qc-ex/01-func.hs QuickCheck - Random A Repeating Pattern Property-based Testing Arnold Functions are defined using pattern matching on the Schwaighofer argument type(s). Haskell - A Truly (Cool) Functional Programming data List a = Empty | Prepend a (List a) Language What - Functional List a ≡ [a] Programming How - Functional Empty ≡ [] Programming Why - Functional Prepend x xs ≡ x : xs Programming Quickcheck - A Truly Cool Property Based Testing Tool [1, 2, 3, 4] ≡ 1 :(2 :(3 :(4 : [ ]))) Summary Custom Random Data Generators len :: [a] → Integer Resources len [ ] = 0 References len (x : xs) = 1 + len xs file:///Users/arnold/Desktop/qc-ex/02-pattern.hs QuickCheck - Random The Beauty Of Being Lazy Property-based Testing Functions are lazy. Only evaluate when result is needed. Arnold Schwaighofer allNumbersFrom :: Integer → [Integer ] Haskell - A Truly allNumbersFrom x = x : allNumbersFrom (x + 1) (Cool) Functional Programming Language What - Functional first :: [Integer ] → Integer Programming How - Functional first [ ] = [ ] Programming Why - Functional Programming first (x : xs) = x Quickcheck - A Truly Cool Property Based Testing Tool take :: Integer → [a] → [a] Summary take 0 = [ ] Custom Random take [ ] = [ ] Data Generators take n (x : xs) = x :(take (n − 1) xs) Resources References first (allNumbersFrom 1) ≡ 1 take 5 (allNumbersFrom 1) ≡ [1, 2, 3, 4, 5] file:///Users/arnold/Desktop/qc-ex/03-lazy.hs QuickCheck - Random Advantages Of Functional Programming Property-based Testing Arnold Schwaighofer Haskell - A Truly I Modular programming - higher order functions, (Cool) Functional Programming producer consumer pattern due to laziness [Hug89] Language What - Functional Programming I Conciseness - less to write, less to read How - Functional Programming I Easier to debug - because functions are pure Why - Functional Programming I Easier to test - because functions are pure Quickcheck - A Truly Cool Property Based I Safer - Type checking finds a lot of errors before Testing Tool even running the program [Car97] Summary Typed Lambda Calculus [Chu36]- mathematical Custom Random I Data Generators theory more beautiful than Turing Machine (to me at Resources least) References QuickCheck - Random QuickCheck [CH00] What Is It About? Property-based Testing Arnold Schwaighofer Haskell - A Truly I Traditionally test cases are written by hand (Unit (Cool) Functional Programming testing) Language Quickcheck - A I Tedious work - remember we are in a lazy setting Truly Cool Property Based I Idea: Functional setting, dependency only on Testing Tool What - Random Property arguments Based Testing How - Random Property Based Testing I Specify properties of a function (e.g f (x) > 0 for all x) Why - Random Property Based Testing Randomly generate arguments and check that Success Stories I Related Work And Outlook property holds Summary We can do this even for arguments that are functions Custom Random I Data Generators (remember Haskell is cool) Resources References QuickCheck - Random Property Based Testing Property-based Testing Arnold Schwaighofer Test case Function Haskell - A Truly (Cool) Functional generator under test Programming Language Quickcheck - A Truly Cool Property Based Testing Tool What - Random Property Based Testing How - Random Property Based Testing Why - Random Property Based Testing Success Stories Property Related Work And Outlook Summary Custom Random Data Generators Resources OK? [Cla06] References QuickCheck - Random Simple Tests Property-based Testing Random test generators for most build-in types (Integers, Arnold Schwaighofer Boolean, Tuples, Lists) are predefined. Haskell - A Truly (Cool) Functional prop_RevUnit :: Integer → Bool Programming Language prop_RevUnit x = Quickcheck - A reverse [x ] ≡ [x ] Truly Cool Property Based Testing Tool What - Random Property Based Testing prop_RevRev :: [Integer ] → Bool How - Random Property Based Testing Why - Random Property prop_RevRev xs = Based Testing Success Stories reverse (reverse xs) ≡ xs Related Work And Outlook Summary Custom Random Data Generators prop_RevApp :: [Integer ] → [Integer ] → Bool Resources prop_RevApp xs ys = References reverse (xs ++ ys) ≡ reverse ys ++ reverse xs file:///Users/arnold/Desktop/qc-ex/04-qcsimple.hs QuickCheck - Random We Don’t Stop At Functions ’Cause Property-based Testing Remember We Are Cool Arnold Schwaighofer Haskell - A Truly Extensionality on functions. (Cool) Functional Programming Language (f === g) x = f x ≡ g x Quickcheck - A Truly Cool Property Based To show function composition is associative. Testing Tool What - Random Property Based Testing How - Random Property prop_CompositionAssociative :: (Int → Int) → Based Testing Why - Random Property (Int → Int) → Based Testing Success Stories (Int → Int) → Related Work And Outlook Summary Int → Bool Custom Random prop_CompositionAssociative f g h = Data Generators f ◦ (g ◦ h) === (f ◦ g) ◦ h Resources References file:///Users/arnold/Desktop/qc-ex/05-qcfunc.hs QuickCheck - Random Observing What Is Going On Property-based Testing There are combinators that can be used in specifications Arnold Schwaighofer that tell us what is going on. Haskell - A Truly (Cool) Functional prop_Insert :: Int → [Int ] → Property Programming Language prop_Insert x xs = Quickcheck - A ordered xs ==> Truly Cool Property Based collect (length xs) \$ Testing Tool What - Random Property ordered (insert x xs) Based Testing How - Random Property Based Testing Why - Random Property Based Testing OK, passed 100 tests. Success Stories Related Work And Outlook 20% 0. Summary 10% 1. Custom Random Data Generators 9% 3. Resources ... References 1% 16. file:///Users/arnold/Desktop/qc-ex/06-monitor.hs QuickCheck - Random Generating Random Data Property-based Testing Arnold Schwaighofer Haskell - A Truly (Cool) Functional Programming Language QuickCheck provides support for user defined random Quickcheck