Property testing with QuickCheck

State a property

A unit test checks one input. A property states something true for every input, and the library generates the inputs for you:

import Test.QuickCheck prop_reverseTwice :: [Int] -> Bool prop_reverseTwice xs = reverse (reverse xs) == xs -- ghci> quickCheck prop_reverseTwice -- +++ OK, passed 100 tests.

One hundred random lists, including empty ones and lists with repeated values, and every one of them passed. That is a much stronger statement than three hand-picked examples.

Counterexamples and shrinking

When a property fails, QuickCheck reports the smallest input that still fails. Shrinking is what makes the report useful:

import Data.List (sort) prop_sorted :: [Int] -> Bool prop_sorted xs = sort xs == xs -- *** Failed! Falsified (after 5 tests and 1 shrink): -- [1,0]

The property is wrong on purpose, and the counterexample is minimal: a two-element list in the wrong order. How many tests ran before the failure changes with the random seed; the shrunk counterexample does not. Without shrinking the report could be a twenty-element list with one inversion buried inside.

Custom generators

Random values only cover the shapes the library knows. A generator for your own type plus a shrinker gives the same treatment to domain data:

import Test.QuickCheck.Arbitrary data Age = Age Int deriving (Show, Eq) instance Arbitrary Age where arbitrary = Age <$> choose (0, 150) shrink (Age n) = [Age n2 | n2 <- shrink n, n2 >= 0]

Constraining the range in arbitrary is better than filtering with ==>, because a filter that rejects most values makes the run slow and the coverage poor.

QuickCheck and HUnit

Properties and examples belong in the same suite. QuickCheck exposes a property as a test case, so the two styles run together:

import Test.HUnit import Test.QuickCheck import Todo (countItems) suite = TestList [ "reverse twice" ~: TestCase (quickCheck prop_reverseTwice) , "empty count" ~: (countItems [] ~?= 0) ]

Use examples for the cases you know are tricky and properties for the invariants you hope hold everywhere. The next lesson explains why laziness makes those invariants cheaper to check.

The code on this page was run with GHC 9.10.3, QuickCheck 2.19.0.0 and HUnit 1.6.2.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev