我对Haskell比较陌生,所以如果我的术语不太正确,请提前道歉。
我想为一个非常简单的项目实现一些普通的单元测试,通过cabal
管理。我注意到这个非常相似的问题,但它没有真正帮助。这个也没有(它提到了tasty
,见下文)。
我认为我可以通过只使用HUnit
来完成这一点-然而,我承认我对所有其他"事情"有点困惑。在网上的指南中谈到:
- 我不太欣赏
exitcode-stdio-1.0
和detailed-0.9
接口之间的差异 - 我不确定使用
HUnit
或Quickcheck
或其他的差异(或中期和长期)含义? - HUnit指南中提到的
tasty
的作用是什么
所以,我试图留下所有的"额外的"将包排除在等式之外,并将其他所有内容设置为"默认"。尽我所能,我做了以下的事情:
$ mkdir example ; mkdir example/test
$ cd example
$ cabal init
然后编辑example.cabal
并添加以下部分:
Test-Suite test-example
type: exitcode-stdio-1.0
hs-source-dirs: test, app
main-is: Main.hs
build-depends: base >=4.15.1.0,
HUnit
default-language: Haskell2010
然后创建test/Main.hs
,内容如下:
module Main where
import Test.HUnit
tests = TestList [
TestLabel "test2"
(TestCase $ assertBool "Why is this not running," False)
]
main :: IO ()
main = do
runTestTT tests
return ()
最后,我试着跑了一遍:
$ cabal configure --enable-tests && cabal build && cabal test
Up to date
Build profile: -w ghc-9.2.4 -O1
In order, the following will be built (use -v for more details):
- example-0.1.0.0 (test:test-example) (additional components to build)
Preprocessing test suite 'test-example' for example-0.1.0.0..
Building test suite 'test-example' for example-0.1.0.0..
Build profile: -w ghc-9.2.4 -O1
In order, the following will be built (use -v for more details):
- example-0.1.0.0 (test:test-example) (ephemeral targets)
Preprocessing test suite 'test-example' for example-0.1.0.0..
Building test suite 'test-example' for example-0.1.0.0..
Running 1 test suites...
Test suite test-example: RUNNING...
Test suite test-example: PASS
Test suite logged to:
/home/jir/workinprogress/haskell/example/dist-newstyle/build/x86_64-linux/ghc-9.2.4/example-0.1.0.0/t/test-example/test/example-0.1.0.0-test-example.log
1 of 1 test suites (1 of 1 test cases) passed.
和输出不是我所期望的。我显然在做一些根本错误的事情,但我不知道是什么。
为了让exitcode-stdio-1.0
测试类型识别出失败的套件,您需要安排您的测试套件的main
函数在出现任何测试失败时退出。幸运的是,有一个runTestTTAndExit
函数来处理这个问题,所以如果您将main
替换为:
main = runTestTTAndExit tests
应该没问题。