2015-10-02 57 views
8

如果我有test/Test.hs當我的測試失敗時,爲什麼我的HUnit測試套件會通過?

module Main where 

import Test.HUnit 

test1 :: Test 
test1 = TestCase $ assertEqual "Should be one" 1 5 

test2 :: Test 
test2 = TestCase $ assertEqual "Shold both be zero" 0 0 

main :: IO Counts 
main = runTestTT $ TestList [test1, test2, test1] 

test-suite my-test 
    type:    exitcode-stdio-1.0 
    hs-source-dirs:  test 
    main-is:   Test.hs 
    build-depends:  base >= 4.8.1.0 && <4.9, 
         HUnit >= 1.3 
    default-language: Haskell2010 

一個.cabal,我跑cabal test --show-details='always'然後我得到

Test suite my-test: RUNNING... 
### Failure in: 0 
test/Test.hs:6 
Should be one 
expected: 1 
but got: 5 
### Failure in: 2 
test/Test.hs:6 
Should be one 
expected: 1 
but got: 5 
Cases: 3 Tried: 3 Errors: 0 Failures: 2 
Test suite my-test: PASS 

爲什麼我的測試套件過程,當我有失敗?同樣,如果我cabal sdist我不會收到我的測試失敗的警告。

回答

4

按照Cabal users' guide,使用exitcode-stdio-1.0接口

測試套件是指示具有非零退出代碼測試失敗時運行的可執行文件;他們可以通過標準輸出和錯誤通道提供人類可讀的日誌信息。

您已經定義

main :: IO Counts 
main = runTestTT $ TestList [test1, test2, test1] 

這種運行測試,打印出測試信息,然後總是成功退出。如果要讓Cabal知道測試失敗,則需要捕獲Counts,檢查errorsfailures,如果發現此情況,請以非零狀態退出。

main :: IO() 
main = do 
    results <- runTestTT $ TestList [test1, test2, test1] 
    if (errors results + failures results == 0) 
    then 
     exitWith ExitSuccess 
    else 
     exitWith (ExitFailure 1) 

test-framework包提供方便defaultMain函數,做這樣的事情;你可能想要考慮這種方法。

您應該注意exitcode-stdio-1.0接口被認爲是半棄用的; Cabal的維護人員建議切換到他們更接近的Haskellian detailed-0.9接口。

+0

我曾經研究過'detailed-0.9',但[得到了氛圍](http://stackoverflow.com/a/18686329/656912),它不夠穩定和難以使用。 – orome

+0

@raxacoricofallapatorius,可能。我從來都沒有搞錯過;我只在現有項目中對現有測試套件進行了一些擴展。 – dfeuer

+0

@dfeuer:我使用了'ExitSuccess'和'ExitFailure'的方法,但是如果我運行'cabal test --show-details ='always'',那麼它不起作用 - build說測試套件:PASS有一個失敗。你能推薦一個解決方法嗎? – altern

相關問題