Hspec: A Testing Framework for Haskell

Contents

Using QuickCheck with Hspec

You can use arbitrary QuickCheck properties with Hspec, but they must be of type Property. QuickCheck's property function can be used to turn anything that is a member of the Testable class into a Property. Here is an example:

describe "read" $ do
  it "is inverse to show" $ property $
    \x -> (read . show) x == (x :: Int)
Example code:
-- file Spec.hs
import Test.Hspec
import Test.QuickCheck

main :: IO ()
main = hspec $ do
  describe "read" $ do
    context "when used with ints" $ do
      it "is inverse to show" $ property $
        \x -> (read . show) x == (x :: Int)
runhaskell Spec.hs

read
  when used with ints
    is inverse to show

Finished in 0.0005 seconds
1 example, 0 failures

It's also possible to modify some of the arguments passed to the Quickcheck driver, namely: the maximum number of successes before succeeding, the maximum number of discarded tests per successful test before giving up and size of the test case:

import Test.Hspec.Core.QuickCheck (modifyMaxSize)

describe "read" $ do
  modifyMaxSize (const 1000) $ it "is inverse to show" $ property $
    \x -> (read . show) x == (x :: Int)