PutText在haskell HUnit中

时间:2016-08-11 20:22:20

标签: haskell hunit

我最近一直在使用HUnit测试框架来运行单元 在haskell进行测试。

我遇到了这个函数PutText和runTestText PutText st作为它的第一个参数。

但是我不知道如何使用它,并希望了解如何使用它?

1 个答案:

答案 0 :(得分:3)

PutText值允许您自定义报告运行测试时生成的消息的方式。

创建一个的简单方法是使用putTextToHandle stdout True将消息输出到标准输出。 True参数表示也会发出进度消息。

PutText协议允许您维护状态。这是一个跟踪发出的消息数量的示例。最终价值 作为第二个组件,runTestText也返回此状态 返回的元组。

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

然后你可以像这样使用它:

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

这里testCounts是运行/通过/失败等测试次数的计数。msgCount是最后一次调用PutText函数返回的值。