Cabal、HTF 和 HUnit 断言的怪异
所以我尝试使用 HTF 来运行一些 HUnit 风格的断言
% cat tests/TestDemo.hs
{-# OPTIONS_GHC -Wall -F -pgmF htfpp #-}
module Main where
import Test.Framework
import Test.HUnit.Base ((@?=))
import System.Environment (getArgs)
-- just run some tests
main :: IO ()
main = getArgs >>= flip runTestWithArgs Main.allHTFTests
-- all these tests should fail
test_fail_int1 :: Assertion
test_fail_int1 = (0::Int) @?= (1::Int)
test_fail_bool1 :: Assertion
test_fail_bool1 = True @?= False
test_fail_string1 :: Assertion
test_fail_string1 = "0" @?= "1"
test_fail_int2 :: Assertion
test_fail_int2 = [0::Int] @?= [1::Int]
test_fail_string2 :: Assertion
test_fail_string2 = "true" @?= "false"
test_fail_bool2 :: Assertion
test_fail_bool2 = [True] @?= [False]
,当我使用 ghc --make 时,它似乎工作正常。
% ghc --make tests/TestDemo.hs
[1 of 1] Compiling Main ( tests/TestDemo.hs, tests/TestDemo.o )
Linking tests/TestDemo ...
% tests/TestDemoA
...
* Tests: 6
* Passed: 0
* Failures: 6
* Errors: 0
Failures:
* Main:fail_int1 (tests/TestDemo.hs:9)
* Main:fail_bool1 (tests/TestDemo.hs:12)
* Main:fail_string1 (tests/TestDemo.hs:15)
* Main:fail_int2 (tests/TestDemo.hs:19)
* Main:fail_string2 (tests/TestDemo.hs:22)
* Main:fail_bool2 (tests/TestDemo.hs:25)
但是当我使用 cabal 构建它时,并不是所有应该失败的测试都会失败。
% cat Demo.cabal
...
executable test-demo
build-depends: base >= 4, HUnit, HTF
main-is: TestDemo.hs
hs-source-dirs: tests
% cabal configure
Resolving dependencies...
Configuring Demo-0.0.0...
% cabal build
Preprocessing executables for Demo-0.0.0...
Building Demo-0.0.0...
[1 of 1] Compiling Main ( tests/TestDemo.hs, dist/build/test-demo/test-demo-tmp/Main.o )
Linking dist/build/test-demo/test-demo ...
% dist/build/test-demo/test-demo
...
* Tests: 6
* Passed: 3
* Failures: 3
* Errors: 0
Failures:
* Main:fail_int2 (tests/TestDemo.hs:23)
* Main:fail_string2 (tests/TestDemo.hs:26)
* Main:fail_bool2 (tests/TestDemo.hs:29)
出了什么问题以及如何修复它?
So I'm trying to use HTF to run some HUnit-style assertions
% cat tests/TestDemo.hs
{-# OPTIONS_GHC -Wall -F -pgmF htfpp #-}
module Main where
import Test.Framework
import Test.HUnit.Base ((@?=))
import System.Environment (getArgs)
-- just run some tests
main :: IO ()
main = getArgs >>= flip runTestWithArgs Main.allHTFTests
-- all these tests should fail
test_fail_int1 :: Assertion
test_fail_int1 = (0::Int) @?= (1::Int)
test_fail_bool1 :: Assertion
test_fail_bool1 = True @?= False
test_fail_string1 :: Assertion
test_fail_string1 = "0" @?= "1"
test_fail_int2 :: Assertion
test_fail_int2 = [0::Int] @?= [1::Int]
test_fail_string2 :: Assertion
test_fail_string2 = "true" @?= "false"
test_fail_bool2 :: Assertion
test_fail_bool2 = [True] @?= [False]
And when I use ghc --make
, it seems to work correctly.
% ghc --make tests/TestDemo.hs
[1 of 1] Compiling Main ( tests/TestDemo.hs, tests/TestDemo.o )
Linking tests/TestDemo ...
% tests/TestDemoA
...
* Tests: 6
* Passed: 0
* Failures: 6
* Errors: 0
Failures:
* Main:fail_int1 (tests/TestDemo.hs:9)
* Main:fail_bool1 (tests/TestDemo.hs:12)
* Main:fail_string1 (tests/TestDemo.hs:15)
* Main:fail_int2 (tests/TestDemo.hs:19)
* Main:fail_string2 (tests/TestDemo.hs:22)
* Main:fail_bool2 (tests/TestDemo.hs:25)
But when I use cabal to build it, not all the tests that should fail, fail.
% cat Demo.cabal
...
executable test-demo
build-depends: base >= 4, HUnit, HTF
main-is: TestDemo.hs
hs-source-dirs: tests
% cabal configure
Resolving dependencies...
Configuring Demo-0.0.0...
% cabal build
Preprocessing executables for Demo-0.0.0...
Building Demo-0.0.0...
[1 of 1] Compiling Main ( tests/TestDemo.hs, dist/build/test-demo/test-demo-tmp/Main.o )
Linking dist/build/test-demo/test-demo ...
% dist/build/test-demo/test-demo
...
* Tests: 6
* Passed: 3
* Failures: 3
* Errors: 0
Failures:
* Main:fail_int2 (tests/TestDemo.hs:23)
* Main:fail_string2 (tests/TestDemo.hs:26)
* Main:fail_bool2 (tests/TestDemo.hs:29)
What's going wrong and how can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 GHC 某些版本中的一个错误,与优化器在某些情况下删除抛出 IO 异常有关。如果启用了优化,这在 HUnit 代码中很常见。 cabal 默认设置
-O1
,这会导致该错误。升级到 GHC 7 修复了这个问题(在库赶上之前并不真正推荐,IE 为 GHC 7 制作了一个 haskell 平台版本)。
您还可以将
-O0
放入与测试可执行文件相关的 .cabal 文件节的编译器选项中。这就是我为测试代码所做的事情,直到我准备好将项目迁移到 GHC 7。This is a bug in certain versions of GHC, related to the optimizer removing throwing IO exceptions in some cases. It is very common with HUnit code, if optimizations are enabled. And cabal sets
-O1
by default, which enables the bug.Upgrading to GHC 7 fixes it (not really recommended until libraries have caught up with it, IE a haskell platform release is made for GHC 7).
You can also put
-O0
in the compiler options in the .cabal file stanza related to your test executable. This is what I've done for my test code, until I am ready to move my project to GHC 7.