46

I have a Haskell project and Cabal package-description that allows me to build and install my package with

$ cabal configure
$ cabal build
$ cabal install

But what about cabal test? Cabal's help says to configure a test suite with UserHooks, but how can I do that?

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245

2 Answers2

33

As Duncan mentioned in Greg's answer, Cabal-1.10 has support for test suites out of the box.

The manual seems to have the best information that's I've found regarding how to make use of this.

Here's a snippet from the manual which shows how to use the exitcode-stdio test type:

foo.cabal

Name:           foo
Version:        1.0
License:        BSD3
Cabal-Version:  >= 1.9.2
Build-Type:     Simple

Test-Suite test-foo
    type:       exitcode-stdio-1.0
    main-is:    test-foo.hs
    build-depends: base

test-foo.hs:

module Main where

import System.Exit (exitFailure)

main = do
    putStrLn "This test always fails!"
    exitFailure
Rudy Matela
  • 6,310
  • 2
  • 32
  • 37
Jacob Stanley
  • 4,704
  • 3
  • 33
  • 36
  • Hmm, looks like a typo in the manual. Shouldn't that read `Cabal-Version: >= 1.10` ? Or will earlier cabals harmlessly ignore it? – Joey Adams May 08 '12 at 22:13
  • Err, nevermind, it says it right there in the manual: "Using test suite sections requires at least Cabal version 1.9.2" – Joey Adams May 08 '12 at 23:14
  • 3
    note that in order for the test suite's dependencies to *install*, you must run `cabal install --enable-tests`. Then you can run `cabal test` to run your tests. – limp_chimp Aug 05 '14 at 00:37
13

For one approach, see Setting up a simple test with Cabal.

This approach has drawbacks, and there's an open Cabal ticket that suggests being able to specify tests more directly, e.g.,

test
  test-is: Test
  build-depends: QuickCheck
  hs-source-dirs: tests src
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245