gpt4 book ai didi

unit-testing - 尽管有测试失败,但运行 “cabal test” 通过

转载 作者:行者123 更新时间:2023-12-04 17:17:57 25 4
gpt4 key购买 nike

我有一个 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

我究竟做错了什么?

最佳答案

main必须是 IO a 类型在哪里 a可以是任何东西,并且该值与程序的 POSIX 退出状态没有任何关系。您需要查看 CountsrunTest 返回并明确选择成功或失败的输出代码。

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

关于unit-testing - 尽管有测试失败,但运行 “cabal test” 通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31492666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com