gpt4 book ai didi

unit-testing - 如何有条件地拆除Tasty中的testbed资源?

转载 作者:行者123 更新时间:2023-11-28 21:19:14 24 4
gpt4 key购买 nike

我使用 Haskell 的 Tasty 框架进行测试。当我获取和清除资源时,我使用 withResource Tasty 的函数:

withResource :: IO a -> (a -> IO ()) -> (IO a -> TestTree) -> TestTree

其中 a 是资源类型。但是我想在测试失败时保留资源,只有在测试通过时才清除它们。怎么可能?

最佳答案

测试失败(至少在 tasty-hunit 中)是 implemented as exceptions . withResourcebracket 的用途就是即使有异常也要释放资源。如果您像这样编写直线代码,当且仅当断言通过时,资源才会被释放:

testCase "resource management" $ do
a <- allocate
assertBool =<< runTest
cleanUp a

关于unit-testing - 如何有条件地拆除Tasty中的testbed资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54129190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com