简体   繁体   中英

PutText in haskell HUnit

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM