Haskell 用SmallCheck和Tasty进行不纯性质测试:资源获取
我正在尝试使用Tasty库和SmallCheck编写基于属性的测试。但是我需要属性检查函数中的IO,也需要I/O资源。因此,我将现有测试转换为:Haskell 用SmallCheck和Tasty进行不纯性质测试:资源获取,haskell,smallcheck,Haskell,Smallcheck,我正在尝试使用Tasty库和SmallCheck编写基于属性的测试。但是我需要属性检查函数中的IO,也需要I/O资源。因此,我将现有测试转换为: myTests :: IO Cfg -> TestTree myTests getResource = testGroup "My Group" [ testProperty "MyProperty" $ -- HOW TO CALL getResource here, but not in -- function, so
myTests :: IO Cfg -> TestTree
myTests getResource = testGroup "My Group"
[
testProperty "MyProperty" $
-- HOW TO CALL getResource here, but not in
-- function, so to avoid multiple acquisition
-- Some{..} <- getResource
\(x::X) -> monadic $ do -- HERE I WILL DO I/O...
]
myTests::IO Cfg->TestTree
myTests getResource=testGroup“我的组”
[
testProperty“MyProperty”$
--如何在此处调用getResource,而不是在中
--功能,以避免多次采集
--一些{..}一元$do——在这里我将执行I/O。。。
]
所以,问题是:如何调用getResource一次?因此,不是在\(x::x)->…
主体中,而是在它之前。可能吗?您可以使用。根据文档,它会将您的IO Cfg
转换为IO Cfg
,从而生成一个资源“将只获取一次,并在树中的所有测试中共享。”
它还提供了一个Cfg->IO()
函数,如果需要,可以在其中释放Cfg
值。由于我不知道您的资源的性质,所以我暂时将该函数作为禁止操作(\cfg->pure()
)保留在这里
myTests :: IO Cfg -> TestTree
myTests getResource =
withResource getResource (\cfg -> pure ()) $ \getResource' ->
testGroup "My Group"
[
testProperty "MyProperty" $ \(x::X) -> monadic $ do
Some{..} <- getResource'
-- DO I/O...
]
myTests::IO Cfg->TestTree
myTests getResource=
withResource getResource(\cfg->pure())$\getResource'->
测试组“我的组”
[
testProperty“MyProperty”$\(x::x)->一元$do
有些{..}是的,但问题是不同的:假设您有多个testProperty
,所以getResource'
将在每个属性中调用,但我的目标是为所有属性调用一次them@Paul-在这种情况下,您可以在整个testGroup
调用中放置withResource
调用。请参阅我的编辑