2013-08-20 24 views
5

我得到單元測試在cabal下運行的難度令人驚訝。我從the cabal documentation逐字複製測試代碼,以更改模塊名稱如何在cabal測試中使用detailed-0.9

{-# 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中導入它們,但它表示它們未被導出。這很簡單,我必須做一些愚蠢的事情,但我看不到它是什麼。

+0

'TestOptions'等人似乎指的是快速檢查的一個古老的舊版本。我建議你使用一個現代測試框架(看起來你正在看的只是一個通過cabal運行測試套件的框架,而不是構建實際套件 - 學習美味或測試框架)。 –

回答

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 
     } 
3

那裏沒有太多支持detailed-0.9。可以掛接現有的測試庫來使用它,但即使如此,測試通過後也無法獲得進度信息。

我建議在開發過程中使用exitcode-stdio-1.0接口和現有的測試框架+使用GHCi。

Hspec的完整示例在這裏https://github.com/sol/hspec-example