Help us understand the problem. What is going on with this article?

Test.HUnit

More than 5 years have passed since last update.

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}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away