LoginSignup
1
1

More than 5 years have passed since last update.

Test.HUnit

Last updated at Posted at 2013-01-06

Assertion は単なる IO ()なので、なんかアクションを書いてコンストラクタであるTestCaseへ渡せば、Test型のデータを作れる。Test型のデータはrunTestTTで実行するのがお手軽(ターミナルに結果を表示)。後Test型にはTestListってコンストラクタもあり、これを使って複数のテストを1つにまとめてもよい。もしくはAssertionの段階で >> で連結して1つのIO ()にまとめても無問題。

Assertion は、IOアクションが普通に終われば成功扱いとなる。テストの失敗を知らせるにはHUnitFailureを例外として投げればいいのだけど、こいつは隠蔽されているので実際はassertFailureを呼べばよい。

% runghc
module Main (main) where
import Test.HUnit

test1, test2 :: Test
test1 = TestCase $ return ()
test2 = TestCase $ assertFailure "FAILED"

testN :: Int -> Test
testN = TestCase . assertBool "I don't like 13" . (/= 13)

tests :: Test
tests = TestList . ([test1, test2] ++) $ (map testN [1 .. 20])

main :: IO ()
main = do
  count <- runTestTT tests
  putStrLn ("The result is " ++ show count)
  return ()

### Failure in: 1
FAILED
### Failure in: 14
I don't like 13
Cases: 22  Tried: 22  Errors: 0  Failures: 2
The result is Counts {cases = 22, tried = 22, errors = 0, failures = 2}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1