[Haskell-cafe] Running tests in Cabal

Rogan Creswick creswick at gmail.com
Sun Jul 18 15:55:34 EDT 2010

On Sun, Jul 18, 2010 at 12:23 PM, Magnus Therning <magnus at therning.org> wrote:
> In my Cabal file I have defined a flag that controls whether tests are
> built or not.  Now I'd like to hook it up a bit more so that './Setup.hs
> test' actually runs the tests.

This will allow you to issue 'cabal test' to run the test suite, but
it may introduce other issues.  I suspect that this won't work well
with dependencies or language extensions defined in the cabal file --
I'm really not sure, as I haven't actually done this in "real" code.
I just wanted to see if it would work a few months back.

In the cabal file, set:

   Build-type:          Custom

Then, set the runTests user hook to the function that invokes your test suite:

#!/usr/bin/env runhaskell
import Distribution.Simple

 -- The module with your test function, eg: 'testSuite' (see below)
import Tests

main = do
  putStrLn "in Main\n"
  defaultMainWithHooks userHooks

-- I think the @testSuite@ function should have type Args -> Bool ->
PackageDescription -> LocalBuildInfo -> IO (), but I'm not 100% sure
of that.  (it does take 4 args, which my sample just ignores)

userHooks :: UserHooks
userHooks = simpleUserHooks { runTests=testSuite }

Here's my Tests.testSuite fn:

testSuite _ _ _ _ = do
  runTestTT $ TestList
    [ testSimple1
    , testSimple2
  return ()

You should now be able to issue 'cabal test' to run the testSuite fn
after configuring.  I can send the whole sample project if you'd like.


More information about the Haskell-Cafe mailing list