简体   繁体   English

如何在美味测试中进行IO?

[英]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. Tasty支持资源,因此我可以获取资源,(完全)测试与该资源相关的一些数据,然后释放该资源。 But how to do some IO actions in my test function? 但是如何在测试功能中执行一些IO操作?

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 . 而且我需要在testWithFoo进行API调用,因此它不能仅返回Assertion而必须在IO How to do it? 怎么做? Or tasty support only pure tests? 还是美味的支持仅是纯测试?

According to the tasty-hunit documentation , Assertion is a type synonym for IO () . 根据美味单位文档AssertionIO ()的类型同义词。

type Assertion = IO ()

This means that you can make your API call inside the test without issue. 这意味着您可以在测试内进行API调用而不会出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM