如何在cabal测试中使用详细的0.9

时间:2013-08-20 19:14:19

标签: testing haskell cabal

我在让单元测试在cabal下运行时遇到了惊人的困难。我已经从the cabal documentation逐字复制了测试代码,但更改模块名称

除外
{-# LANGUAGE FlexibleInstances #-}
module Test.Integral ( tests ) where

import Distribution.TestSuite

instance TestOptions (String, Bool) where
    name = fst
    options = const []
    defaultOptions _ = return (Options [])
    check _ _ = []

instance PureTestable (String, Bool) where
    run (name, result) _ | result == True = Pass
                         | result == False = Fail (name ++ " failed!")

test :: (String, Bool) -> Test
test = pure

-- In actual usage, the instances 'TestOptions (String, Bool)' and
-- 'PureTestable (String, Bool)', as well as the function 'test', would be
-- provided by the test framework.

tests :: [Test]
tests =
    [ test ("bar-1", True)
    , test ("bar-2", False)
    ]

但是,当我尝试构建测试时,我收到以下消息:

Test/Integral.hs:6:10:
    Not in scope: type constructor or class `TestOptions'

Test/Integral.hs:12:10:
    Not in scope: type constructor or class `PureTestable'

我尝试直接从Distribution.TestSuite导入它们,但它说它们没有导出。这很简单,我必须做一些愚蠢的事,但我看不出它是什么。

2 个答案:

答案 0 :(得分:5)

但是对于它的价值,这里有一些有用的代码:

module Main (tests) where

import Distribution.TestSuite

tests :: IO [Test]
tests = do
  return [   
      test "foo" Pass
    , test "bar" (Fail "It did not work out!")
    ]

test :: String -> Result -> Test
test name r = Test t
  where          
    t = TestInstance {
        run = return (Finished r)
      , name = name
      , tags = []
      , options = []
      , setOption = \_ _ -> Right t
      }

答案 1 :(得分:3)

那里detailed-0.9的支持不多。可以连接现有的测试库来使用它,但即使这样,当测试通过时你也不会获得进度信息。

我建议将exitcode-stdio-1.0接口与现有测试框架一起使用+在开发过程中使用GHCi。

Hspec的完整示例位于https://github.com/sol/hspec-example

相关问题