Hspec is a Behaviour-Driven Development tool for Haskell programmers. BDD is an approach to software development that combines Test-Driven Development, Domain Driven Design, and Acceptance Test-Driven Planning. Hspec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD.
Hspec (and the preceding intro) are based on the Ruby library RSpec. Much of what applies to RSpec also applies to Hspec. Hspec ties together descriptions of behavior and examples of that behavior. The examples can also be run as tests and the output summarises what needs to be implemented.
NOTE: There is a monadic and a non-monadic API. This is the documentation for the non-monadic API. The monadic API is more stable, so you may prefer it over this one. For documentation on the monadic API look at Test.Hspec.Monadic.
- data Spec
- type Specs = [Spec]
- class Example a
- data Pending
- describe :: String -> [Spec] -> Spec
- it :: Example a => String -> a -> Spec
- pending :: String -> Pending
- hspec :: Specs -> IO [EvaluatedSpec]
- hspecB :: Specs -> IO Bool
- hspecX :: Specs -> IO a
- hHspec :: Handle -> Specs -> IO [EvaluatedSpec]
- descriptions :: Specs -> Specs
import Test.Hspec import Test.Hspec.QuickCheck import Test.Hspec.HUnit () import Test.QuickCheck import Test.HUnit main = hspecX mySpecs
Since the specs are often used to tell you what to implement, it's best to start with undefined functions. Once we have some specs, then you can implement each behavior one at a time, ensuring that each behavior is met and there is no undocumented behavior.
unformatPhoneNumber :: String -> String unformatPhoneNumber number = undefined formatPhoneNumber :: String -> String formatPhoneNumber number = undefined
mySpecs = [describe "unformatPhoneNumber" [
A boolean expression can act as a behavior's example.
it "removes dashes, spaces, and parenthesies" $ unformatPhoneNumber "(555) 555-1234" == "5555551234" ,
pending function marks a behavior as pending an example. The example
doesn't count as failing.
it "handles non-US phone numbers" $ pending "need to look up how other cultures format phone numbers" ,
it "removes the \"ext\" prefix of the extension" $ TestCase $ do let expected = "5555551234135" actual = unformatPhoneNumber "(555) 555-1234 ext 135" expected @?= actual ,
IO() action is treated like an HUnit
TestCase. (must import
it "converts letters to numbers" $ do let expected = "6862377" actual = unformatPhoneNumber "NUMBERS" actual @?= expected ,
property function allows a QuickCheck property to act as an example.
(must import Test.Hspec.QuickCheck)
it "can add and remove formatting without changing the number" $ property $ forAll phoneNumber $ \n -> unformatPhoneNumber (formatPhoneNumber n) == n ]] phoneNumber :: Gen String phoneNumber = do n <- elements [7,10,11,12,13,14,15] vectorOf n (elements "0123456789")
A type class for examples.
Defining a spec
Create a set of specifications for a specific type being described. Once you know what you want specs for, use this.
describe "abs" [ it "returns a positive number given a negative number" (abs (-1) == 1) ]
A pending example.
If you want to report on a behavior but don't have an example yet, use this.
describe "fancyFormatter" [ it "can format text in a way that everyone likes" $ pending ]
You can give an optional reason for why it's pending.
describe "fancyFormatter" [ it "can format text in a way that everyone likes" $ pending "waiting for clarification from the designers" ]
Running a spec
Create a document of the given specs and write it to stdout.
Create a document of the given specs and write it to the given handle.
writeReport filename specs = withFile filename WriteMode (\h -> hHspec h specs)