[Haskell-cafe] HUnit/cabal integration

Richard Cobbe cobbe at ccs.neu.edu
Wed Jul 4 21:13:36 CEST 2012


I'm working on a little library package (purely for my own consumption)
that's built with Cabal, and I have a couple of questions about the
pragmatics of using HUnit for it.

First, I'd like to be able to run my tests via "cabal test" from the shell
prompt.  I've seen
<http://hackage.haskell.org/trac/hackage/wiki/UpgradingTests> and followed
that, and it basically works.  I'm curious if I'm doing it in the best (or
at least most idiomatic) way.  I've included my cabal file and test driver
below, for specifics.

Two questions:

First: the web page I cite above describes the interface that the test
binary must support to work with cabal, specifically w.r.t. the binary's
exit code.  "Your test suites likely already fit this model.  However, if
you are using an old version of QuickCheck or HUnit, your executable may
not be returning the correct error code."

This seems to me to suggest that recent versions of HUnit automatically
take care of generating the exit code, but I've found that I have to
examine HUnit's results and synthesize the exit code manually, as in the
driver program below.  (I'm running HUnit 1.2.4.2, but the interface for
1.2.4.3 doesn't appear to differ on this point.)  Am I misinterpreting the
wiki page, or am I missing something in HUnit's API that generates the exit
code automatically?

Second: Am I specifying the Build-Depends correctly for the Test-Suite?
Specifically: do I need to state a dependency on the library defined in the
same package, or does it pick that up automatically?  Further, foo-tests
doesn't use parsec directly.  Is the transitive dependency automatically
provided for me, or do I need to list it explicitly as below?

Thanks much,

Richard

My cabal file:

    Name:               foo
    Version:            0.0
    Cabal-Version:      >= 1.2
    Author:             Richard Cobbe
    Synopsis:           Sample cabal package for HUnit integration
    Build-Type:         Simple

    Library
      Exposed-Modules:
        Foo,
        Foo.Parser,
        Foo.Show
      Build-Depends:
        base >= 4.3.1.0 && < 5,
        parsec >= 3.1.2

    Test-Suite foo-tests
      main-is: foo-tests.hs
      type: exitcode-stdio-1.0
      Build-Depends:
        base >= 4.3.1.0 && < 5,
        parsec >= 3.1.2,
        HUnit >= 1.2.4.2

and foo-tests.hs:

    module Main where

    import Test.HUnit
    import qualified Foo.Parser
    import System.Exit

    main :: IO ()
    main =
      do c <- runTestTT ("Foo" ~: Foo.Parser.tests)
         if (errors c == 0 && failures c == 0)
           then exitWith ExitSuccess
           else exitWith (ExitFailure (-1))




More information about the Haskell-Cafe mailing list