[Basic QuickCheck use Don Stewart **20080420233114] { hunk ./en/ch12-testing.xml 35 -In this chapter we'll re-examine the JSON pretty printer developed in -chapter 6, testing it with QuickCheck, and guiding the testing via GHC's -test coverage tool: HPC. +In this chapter we'll look at how to use QuickCheck to establish invariants +in code, and then re-examine the JSON pretty printer developed in chapter 6, +testing it with QuickCheck. We'll also see how to guide the testing process +with GHC's test coverage tool: HPC. hunk ./en/ch12-testing.xml 41 + + QuickCheck: type-based testing + + +To get an overview of how property-based testing works, we'll begin with a +simple scenario: you've written a specialised sort function and want to test +it for desired properties, against a reference implementation, and its +behaviour on different data types. + + + +First, we import the QuickCheck library, and the list library: + + + &QC-basics.hs:module; + + &QC-basics.hs:mysort; + hunk ./en/ch12-testing.xml 61 - Writing QuickCheck properties + Generating arbitrary documents hunk ./examples/ch12/QC-basics.hs 8 -{-- snippet model --} +{-- snippet mysort --} hunk ./examples/ch12/QC-basics.hs 10 -qsort (x:xs) = qsort lhs ++ [x] +++ qsort rhs +qsort (x:xs) = qsort lhs ++ [x] ++ qsort rhs hunk ./examples/ch12/QC-basics.hs 13 -{-- /snippet model --} +{-- /snippet mysort --} }