2015-10-02 54 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

相关问题