HUnitの使い方(1)

HUnitをとりあえず実行してみました。次回、モジュールをテストする方法も説明するつもりです。まずghciを立ち上げてwasrun.hsを作ります。


noriaki@noriaki-laptop:~$ ghci
GHCi, version 6.10.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> :e wasrun.hs

wasrun.hsはテストの成功例と失敗例を書きます。

module WasRun where
import Test.HUnit
testMethod, brokenMethod ::Test
testMethod = TestCase(assertBool "message" True)
brokenMethod = TestCase(assertEqual "message" 1 2)
tests ::Test
tests = TestList [TestLabel "testMethod" testMethod,
                  TestLabel "brokenMethod" brokenMethod
                 ]
main ::IO Counts
main = runTestTT tests

記録してからwasrun.hsをロードし、main関数を実行します。


Prelude> :load wasrun.hs
[1 of 1] Compiling WasRun ( wasrun.hs, interpreted )
Ok, modules loaded: WasRun.
[star]WasRun> main
Loading package HUnit-1.2.0.3 ... linking ... done.
### Failure in: 1:brokenMethod
message
expected: 1
but got: 2
Cases: 2 Tried: 2 Errors: 0 Failures: 1
Counts {cases = 2, tried = 2, errors = 0, failures = 1}

はい、テストの実行に成功しました([star]は本来*で表示されます)。