Hspec: Behavior-Driven Development for Haskell

User's Manual

Hspec is a testing framework for Haskell. It is roughly based on the Ruby library RSpec. Some of Hspec's distinctive features are:

An example is worth a thousand words, so here we go:

cabal update && cabal install hspec
-- file Spec.hs
import Test.Hspec
import Test.QuickCheck
import Control.Exception (evaluate)

main :: IO ()
main = hspec $ do
  describe "Prelude.head" $ do
    it "returns the first element of a list" $ do
      head [23 ..] `shouldBe` (23 :: Int)

    it "returns the first element of an *arbitrary* list" $
      property $ \x xs -> head (x:xs) == (x :: Int)

    it "throws an exception if used with an empty list" $ do
      evaluate (head []) `shouldThrow` anyException
runhaskell Spec.hs

Prelude.head
  - returns the first element of a list
  - returns the first element of an *arbitrary* list
  - throws an exception if used with an empty list

Finished in 0.0028 seconds
3 examples, 0 failures

Table of contents

Other resources

Support

Want to chat? /join #hspec on freenode!