Writing tests for Haskell
There are many libraries for testing Haskell code. Besides classic unit tests with HUnit and spec testing in Ruby-style with Hspec, we can verify properties using SmallCheck and QuickCheck with exhaustive and randomized test cases, respectively.
Property checks
With QuickCheck we can test properties in a randomized fashion. We don't need to generate the test cases ourselves, as QuickCheck takes care of that. Here's a quick example of testing a simple arithmetic property:
stack ghci --package QuickCheck > import Test.QuickCheck as QC > QC.quickCheck $ \x y -> x > 0 ==> x + y >= y
All testable properties in QuickCheck
are given as instances of the Testable
class. As a quick reference, the core interface looks like this:
quickCheck :: Testable prop => prop → IO () class Testable prop where […] instance Testable Property instance Testable Bool instance (Arbitrary a, Show a, Testable prop) => Testable (a → prop)
The...