尽管存在测试失败,但仍会运行“cabal测试”

时间:2015-07-18 15:26:40

标签: unit-testing haskell cabal

我有一个Cabal测试目标:

test-suite Tests
  type:              exitcode-stdio-1.0
  main-is:           Main.hs
  hs-source-dirs:    test, src
  build-depends:     base, …
  default-language:  Haskell2010

一个简单的测试Main.hs

import Test.HUnit
testSanity = TestCase $ assertEqual "Should fail" 2 1
main = runTestTT testSanity

现在正在运行cabal test次:

Test suite Tests: RUNNING...
Test suite Tests: PASS
Test suite logged to: dist/test/Project-0.1.0-Tests.log
1 of 1 test suites (1 of 1 test cases) passed.

即使在测试套件日志中正确记录了失败:

Test suite Tests: RUNNING...

Cases: 1  Tried: 0  Errors: 0  Failures: 0

### Failure:
Should fail
expected: 2
 but got: 1
Cases: 1  Tried: 1  Errors: 0  Failures: 1
Test suite Tests: PASS
Test suite logged to: dist/test/Project-0.1.0-Tests.log

我做错了什么?

2 个答案:

答案 0 :(得分:7)

main必须是IO a类型,其中a可以是任何内容,并且该值不以任何方式与程序的POSIX退出状态相关联。您需要查看Counts返回的runTest,并明确选择成功或失败的输出代码。

import System.Exit

main :: IO ()
main = do
    cs@(Counts _ _ errs fails) <- runTestTT testSanity
    putStrLn (showCounts cs)
    if (errs > 0 || fails > 0) 
        then exitFailure
        else exitSuccess

答案 1 :(得分:3)

我相信<suite name="API TEST CASES"> <test name="api test" parallel="methods"> <groups> <run> <include name="test" /> </run> </groups> <classes> <class name="com.test.test3.TestPlan" /> </classes> </test> 期望测试通过程序的退出代码传达失败。但是,如果您从shell手动运行测试程序(查看exitcode-stdio-1.0目录),我认为您会发现退出代码始终为0。

这是一个相关的SO问题,表明如果您的套房没有通过,您的测试应该致电dist/build

QuickCheck exit status on failures, and cabal integration

FWIW,它在使用exitFailure时正常工作:

stack

也许$ mkdir new-dir $ cd new-dir $ stack new (edit new-template.cabal to add HUnit as a test dependency) (add test case to test/Spec.hs) $ stack test 也在扫描测试输出。