简体   繁体   中英

How to do IO in tasty test?

Tasty supports resources, so I can acquire resource, test some data related to this resource (purely) and to release the resource. But how to do some IO actions in my test function?

This is the example from the documentation:

import Test.Tasty
import Test.Tasty.HUnit

-- assumed defintions
data Foo
acquire :: IO Foo
release :: Foo -> IO ()
testWithFoo :: Foo -> Assertion
(acquire, release, testWithFoo) = undefined

main = do
  defaultMain $
    withResource acquire release tests

tests :: IO Foo -> TestTree
tests getResource =
  testGroup "Tests"
    [ testCase "x" $ getResource >>= testWithFoo
    ]

So my resource is some connection (like socket). And I need to make API call in the testWithFoo , so it can not return just Assertion but it must be in IO . How to do it? Or tasty support only pure tests?

According to the tasty-hunit documentation , Assertion is a type synonym for IO () .

type Assertion = IO ()

This means that you can make your API call inside the test without issue.

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