cabalのreplでテストスイートを使う
Haskell
のテストをcabal
で行う時は、下記のように実行します。(configure
は毎回出なくて大丈夫ですが)
% cabal configure --enable-tests % cabal build % cabal test
テストコードを少し書くごとにcabal build
するのは、テストが蓄積されると時間がかかります。テストコードを書いている時に、repl
でテストコードを試せると便利だと思います。通常では、executable
やlibrary
の依存関係をrepl
は使っていると思います。そこにテスト関連のライブラリの依存関係はもたせたくないということです。
詳しいことはわかっていないのですが、cabal repl
時にTest-Suite
名を指定すると、そのテストスイートの依存関係でのrepl
が起動するようです。
-- Test Test-Suite MyTestSuite01 type: exitcode-stdio-1.0 main-is: MyTest01.hs hs-source-dirs: test ,src -- これは余計で、本来は、cabalのname自体をbuild-dependsに書くべき ghc-options: -Wall build-depends: base >=4.9 && <4.10 , tasty , tasty-hunit , protocol-buffers >=2.4 && <2.5 , protocol-buffers-descriptor >=2.4 && <2.5 , network .. 以下省略
としておいて、
% cabal repl MyTestSuite01
で起動します。
*Main> :l test/MyTest01.hs *Main> main my_test_01: FAIL 1=2 ? expected: 1 but got: 2 1 out of 1 tests failed (0.00s) *** Exception: ExitFailure 1 *Main>
使ったコードは下記です。
module Main where import Test.Tasty import Test.Tasty.HUnit import System.Exit (exitFailure) main =defaultMain (testCase "my_test_01" (assertEqual "1=2 ?" 1 2) )