Typically, any significant Haskell project is started using cabal. It takes care of construction, distribution, documentation (using haddock) and testing.
The standard approach is to put your tests in the test directory and then set up the test suite in your .cabal file. This is described in detail in the user manual . Here is a test suite for one of my projects that looks like
Test-Suite test-melody type: exitcode-stdio-1.0 main-is: Main.hs hs-source-dirs: test build-depends: base >=4.6 && <4.7, test-framework, test-framework-hunit, HUnit, containers == 0.5.*
Then in the file test/Main.hs
import Test.HUnit import Test.Framework import Test.Framework.Providers.HUnit import Data.Monoid import Control.Monad import Utils pushTest :: Assertion pushTest = [NumLit 1] ^? push (NumLit 1) pushPopTest :: Assertion pushPopTest = [] ^? (push (NumLit 0) >> void pop) main :: IO () main = defaultMainWithOpts [testCase "push" pushTest ,testCase "push-pop" pushPopTest] mempty
Where Utils defines nicer interfaces over HUnit .
For easier weight testing, I highly recommend using QuickCheck . This allows you to write short properties and test them against a series of random inputs. For example:
-- Tests.hs import Test.QuickCheck prop_reverseReverse :: [Int] -> Bool prop_reverseReverse xs = reverse (reverse xs) == xs
And then
$ ghci Tests.hs > import Test.QuickCheck > quickCheck prop_reverseReverse .... Passed Tests (100/100)
jozefg Dec 02 '13 at 15:11 2013-12-02 15:11
source share