Summary
In this chapter, we explored testing in Haskell. In particular, we contrasted unit testing with property-based testing in QuickCheck
. The latter allows more thorough testing with less effort, but writing test properties requires a bit more abstraction. We have seen that, to support properties of user-defined types, we should instantiate the Arbitrary
type class to supply a generator for test inputs. Ideally, we also supply a shrinking strategy, which QuickCheck
uses to derive smaller, more manageable counterexamples.
Although still relatively little known outside of the Haskell community, there are clones of the QuickCheck
library available for many other programming languages. Also, for Haskell, several variants exist, such as SmallCheck
, which replaces random generation with systematic enumeration of small values. Moreover, modern testing frameworks such as hspec
combine property-based and unit testing under the same roof.