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: ...

Get Haskell High Performance Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.