Setting up a simple test with Cabal
As an Amazon Associate I earn from qualifying purchases.
With the Cabal build and packaging system for Haskell, add a simple test program to your build with a couple of easy steps.
First, add the following to your project’s cabal file:
Build-Type: Custom
...
flag test
description: Build test program.
default: False
Executable test
hs-source-dirs: src, test
other-modules: MyModule1, MyModule2
main-is: Main.hs
build-depends: base
if !flag(test)
buildable: False
When enabled (via cabal configure -ftest but otherwise off), this builds an extra program called test.
The custom build type gives you more flexibility in your setup script, so add code such as the following to Setup.hs:
main = defaultMainWithHooks hooks
where hooks = simpleUserHooks { runTests = runTests' }
runTests' :: Args -> Bool -> PackageDescription -> LocalBuildInfo -> IO ()
runTests' _ _ _ lbi = system testprog >> return ()
where testprog = (buildDir lbi) </> "test" </> "test"
When you run cabal test, it will kick off your test program whose source is in test/Main.hs.
This approach has a few drawbacks. Users must explicitly enable the test builds. Building the test program entails rebuilding the other libraries in your package. Installing from a -ftest configuration will also install your test program.
Further reading
- Real World Haskell (paid link) by Bryan O’Sullivan, John Goerzen, and Don Stewart: chapters 5, 11, and 26 cover Cabal packages, HUnit, and QuickCheck.
- Haskell in Depth (paid link) by Vitaly Bragilevsky: Cabal packages and testing a Cabal project.
- Practical Haskell (paid link), 2nd edition, by Alejandro Serrano Mena: Cabal projects and testing with HUnit and QuickCheck.