PutText 在 haskell HUnit 中

PutText in haskell HUnit

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

中进行测试

我遇到了这个函数 PutText 和 运行TestText 需要 PutText st 作为它的第一个参数。

但是我不确定如何使用它,需要一些帮助来理解如何使用它?

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 返回的值功能。