[英]Why does my HUnit test suite fail but pass successfully in Cabal?
If I have test/Test.hs
with如果我有test/Test.hs
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]
and a .cabal
with和一个.cabal
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
and I run cabal test --show-details='always'
then I get然后我运行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
Why does my test suite pass when I've had failures?为什么我的测试套件在我失败时通过了? Likewise, if I cabal sdist
I get no warning that my tests have failed.同样,如果我cabal sdist
我不会收到测试失败的警告。
According to the Cabal users' guide ,根据Cabal 用户指南,
Test suites using the
exitcode-stdio-1.0
interface are executables that indicate test failure with a non-zero exit code when run;使用exitcode-stdio-1.0
接口的测试套件是可执行文件,在运行时用非零退出代码指示测试失败; they may provide human-readable log information through the standard output and error channels.它们可以通过标准输出和错误通道提供人类可读的日志信息。
You've defined你已经定义
main :: IO Counts
main = runTestTT $ TestList [test1, test2, test1]
This runs tests, prints out test information, and then always exits successfully.这会运行测试,打印出测试信息,然后总是成功退出。 If you want Cabal to know the test has failed, you need to capture the Counts
, check for errors
and failures
, and exit with a non-zero status if you find such.如果你想让 Cabal 知道测试失败了,你需要捕获Counts
,检查errors
和failures
,如果发现errors
和failures
,并以非零状态退出。
import System.Exit
main :: IO ()
main = do
results <- runTestTT $ TestList [test1, test2, test1]
if (errors results + failures results == 0)
then
exitWith ExitSuccess
else
exitWith (ExitFailure 1)
The test-framework
package provides convenient defaultMain
functions that do this sort of thing; test-framework
包提供了方便的defaultMain
函数来做这种事情; you may want to consider that approach.您可能需要考虑这种方法。
You should note that the exitcode-stdio-1.0
interface is considered semi-deprecated;你应该注意到exitcode-stdio-1.0
接口被认为是半弃用的; the Cabal maintainers recommend switching to their rather more Haskellian detailed-0.9
interface. Cabal 维护者建议切换到他们更像 Haskellian 的 detail detailed-0.9
界面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.