Testing with HUnit
A test suite
A test suite is a second build target in the same .cabal file. It depends on the library and on the test framework, and it has its own main:
test-suite todo-test
type: exitcode-stdio-1.0
main-is: Spec.hs
hs-source-dirs: test
build-depends: base, todo, HUnit >=1.6
default-language: Haskell2010
Keeping the logic in the library is what makes this stanza short. main in the test file only builds and runs the assertions.
Test cases
HUnit offers three ingredients: a label, an assertion with ~?=, and a list of cases:
import Test.HUnit
import Todo (addItem, countItems)
tests = TestList
[ "empty list has no items" ~: (countItems [] ~?= 0)
, "one item" ~: (countItems (addItem "milk" []) ~?= 1)
]
~?= compares two values and reports both sides when they differ. ~: attaches a label, which is the line you will read at two in the morning.
Run the tests
The test main prints a summary and exits non-zero on failure, which is what a continuous integration job needs:
import Control.Monad (when)
import System.Exit (exitFailure)
main :: IO ()
main = do
counts <- runTestTT tests
when (errors counts + failures counts > 0) exitFailure
cabal test
Cases: 2 Tried: 2 Errors: 0 Failures: 0 Test suite todo-test: PASS
cabal test --test-show-details=direct prints the output as it happens instead of hiding it until the end, which matters for a failing case.
Why pure code tests well
Every function in the library is a value transformer, so a test is an expression and needs no setup, no mock, and no teardown:
import Data.Char (toUpper)
shout = map toUpper
-- the whole test is one comparison
(shout "hi" ~?= "HI")
The IO shell from lesson 33 stays thin precisely so that most of the program lands here. Property based testing in the next lesson takes the same advantage one step further.
The code on this page was run with GHC 9.10.3, HUnit 1.6.2.0 and cabal-install 3.16.0.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev