2

I have recently been using the HUnit testing framework to run unit tests in haskell.

I came across this function PutText and runTestText which takes PutText st as its first argument.

However i am not sure how to use this and would like some help in understanding how to use this?

Yusuf
  • 491
  • 2
  • 8

1 Answers1

3

A PutText value allows you to customize the way to messages generated from running a test are reported.

A simple way to create one is use putTextToHandle stdout True to output messages to standard out. The True parameter means to also emit progress messages.

The PutText protocol allows you to maintain state. This is an example of one that keeps track of the number of messages emitted. The final value of this state is also returned by runTestText as the second component of the returned tuple.

reportMsg :: String -> Bool -> Int -> IO Int
reportMsg message isProgress count = do
  putStrLn $ "#" ++ show (count+1) ++ ": " ++ message
  return (count+1)

myPutText = PutText reportMsg 0  :: PutText Int

And then you can use it like this:

(testCounts, msgCount) <- runTestText myPutText tests
putStrLn $ "Messages emitted: " ++ show msgCount

Here testCounts is a tally of the number of tests which were run / passed / failed / etc. The msgCount is the value returned by the last call to the PutText function.

ErikR
  • 51,541
  • 9
  • 73
  • 124
  • Thank you, i was wondering what purpose does the bool argument serve? – Yusuf Aug 11 '16 at 22:27
  • 1
    It indicates whether or not the message is a "progress" message - I presume like reporting the number of tests currently completed. – ErikR Aug 11 '16 at 23:12
  • Whenever i try to use it, it always turns out to be false. Im not sure why, any ideas? – Yusuf Aug 11 '16 at 23:25