|
Packit |
bc3140 |
-- | HUnit is a unit testing framework for Haskell, inspired by the JUnit tool
|
|
Packit |
bc3140 |
-- for Java. This guide describes how to use HUnit, assuming you are familiar
|
|
Packit |
bc3140 |
-- with Haskell, though not necessarily with JUnit.
|
|
Packit |
bc3140 |
--
|
|
Packit |
bc3140 |
-- In the Haskell module where your tests will reside, import module
|
|
Packit |
bc3140 |
-- @Test.HUnit@:
|
|
Packit |
bc3140 |
--
|
|
Packit |
bc3140 |
-- @
|
|
Packit |
bc3140 |
-- import Test.HUnit
|
|
Packit |
bc3140 |
-- @
|
|
Packit |
bc3140 |
--
|
|
Packit |
bc3140 |
-- Define test cases as appropriate:
|
|
Packit |
bc3140 |
--
|
|
Packit |
bc3140 |
-- @
|
|
Packit |
bc3140 |
-- test1 = TestCase (assertEqual "for (foo 3)," (1,2) (foo 3))
|
|
Packit |
bc3140 |
-- test2 = TestCase (do (x,y) <- partA 3
|
|
Packit |
bc3140 |
-- assertEqual "for the first result of partA," 5 x
|
|
Packit |
bc3140 |
-- b <- partB y
|
|
Packit |
bc3140 |
-- assertBool ("(partB " ++ show y ++ ") failed") b)
|
|
Packit |
bc3140 |
-- @
|
|
Packit |
bc3140 |
--
|
|
Packit |
bc3140 |
-- Name the test cases and group them together:
|
|
Packit |
bc3140 |
--
|
|
Packit |
bc3140 |
-- @
|
|
Packit |
bc3140 |
-- tests = TestList [TestLabel "test1" test1, TestLabel "test2" test2]
|
|
Packit |
bc3140 |
-- @
|
|
Packit |
bc3140 |
--
|
|
Packit |
bc3140 |
-- Run the tests as a group. At a Haskell interpreter prompt, apply the function
|
|
Packit |
bc3140 |
-- @runTestTT@ to the collected tests. (The /TT/ suggests /T/ext orientation
|
|
Packit |
bc3140 |
-- with output to the /T/erminal.)
|
|
Packit |
bc3140 |
--
|
|
Packit |
bc3140 |
-- @
|
|
Packit |
bc3140 |
-- \> runTestTT tests
|
|
Packit |
bc3140 |
-- Cases: 2 Tried: 2 Errors: 0 Failures: 0
|
|
Packit |
bc3140 |
-- \>
|
|
Packit |
bc3140 |
-- @
|
|
Packit |
bc3140 |
--
|
|
Packit |
bc3140 |
-- If the tests are proving their worth, you might see:
|
|
Packit |
bc3140 |
--
|
|
Packit |
bc3140 |
-- @
|
|
Packit |
bc3140 |
-- \> runTestTT tests
|
|
Packit |
bc3140 |
-- ### Failure in: 0:test1
|
|
Packit |
bc3140 |
-- for (foo 3),
|
|
Packit |
bc3140 |
-- expected: (1,2)
|
|
Packit |
bc3140 |
-- but got: (1,3)
|
|
Packit |
bc3140 |
-- Cases: 2 Tried: 2 Errors: 0 Failures: 1
|
|
Packit |
bc3140 |
-- \>
|
|
Packit |
bc3140 |
-- @
|
|
Packit |
bc3140 |
--
|
|
Packit |
bc3140 |
-- You can specify tests even more succinctly using operators and overloaded
|
|
Packit |
bc3140 |
-- functions that HUnit provides:
|
|
Packit |
bc3140 |
--
|
|
Packit |
bc3140 |
-- @
|
|
Packit |
bc3140 |
-- tests = test [ "test1" ~: "(foo 3)" ~: (1,2) ~=? (foo 3),
|
|
Packit |
bc3140 |
-- "test2" ~: do (x, y) <- partA 3
|
|
Packit |
bc3140 |
-- assertEqual "for the first result of partA," 5 x
|
|
Packit |
bc3140 |
-- partB y \@? "(partB " ++ show y ++ ") failed" ]
|
|
Packit |
bc3140 |
-- @
|
|
Packit |
bc3140 |
--
|
|
Packit |
bc3140 |
-- Assuming the same test failures as before, you would see:
|
|
Packit |
bc3140 |
--
|
|
Packit |
bc3140 |
-- @
|
|
Packit |
bc3140 |
-- \> runTestTT tests
|
|
Packit |
bc3140 |
-- ### Failure in: 0:test1:(foo 3)
|
|
Packit |
bc3140 |
-- expected: (1,2)
|
|
Packit |
bc3140 |
-- but got: (1,3)
|
|
Packit |
bc3140 |
-- Cases: 2 Tried: 2 Errors: 0 Failures: 1
|
|
Packit |
bc3140 |
-- \>
|
|
Packit |
bc3140 |
-- @
|
|
Packit |
bc3140 |
|
|
Packit |
bc3140 |
module Test.HUnit
|
|
Packit |
bc3140 |
(
|
|
Packit |
bc3140 |
module Test.HUnit.Base,
|
|
Packit |
bc3140 |
module Test.HUnit.Text
|
|
Packit |
bc3140 |
)
|
|
Packit |
bc3140 |
where
|
|
Packit |
bc3140 |
|
|
Packit |
bc3140 |
import Test.HUnit.Base
|
|
Packit |
bc3140 |
import Test.HUnit.Text
|
|
Packit |
bc3140 |
|