Software Testing With Haskell
by ShaeErisson for The Monad.Reader Issue 5
2005-10-02
Abstract.
The two most commonly used libraries for testing software with Haskell are HUnit and QuickCheck. This article will shortly describe the two different approaches and show some demonstration code.
Both HUnit and QuickCheck are included with GHC 6.4.1 under the module names of Test.HUnit and Test.QuickCheck
HUnit
HUnit was written by Dean Herington and is available on sourceforge at http://hunit.sourceforge.net/ .
If you've ever used the
xUnit framework in other programming languages, HUnit will feel familiar. The
User's Guide includes a 'getting started' started section, and there are a thousand introductions to various flavors of the xUnit framework, so we'll mention HUnit only briefly.
From the user's guide:
"Tests are specified compositionally.
Assertions are combined to make a
test case, and test cases are combined into
tests."
Here's a short demo:
module ProtoHunit where
import Test.HUnit
import Test.HUnit
testList = TestList -- construct a TestList from a list of type TestCase
[TestCase $ -- construct a TestCase from an assertion
assertEqual "description" 2 (1 + 1) -- construct an assertion from a descriptive string, an expected result, and something to execute
]
t = runTestTT testList
