[Haskell-cafe] Organizaing tests in Haskell

Simon Hengel sol at typeful.net
Sun Sep 23 12:06:28 CEST 2012


> Is there any better solution to organize tests in Haskell?

(Disclaimer: I'm the maintainer of Hspec ;)

If you use Hspec[1] for testing, you do not have to assemble your
individual tests manually into a test suit; hspec-discover[2] takes care
of that.

There is no comprehensive user's guide for Hspec yet, but a basic
introduction is at [3].  If you have any questions, feel free to join in
at #hspec on freenode.

> Should I just give up on module encapsulation, or should I only test
> functions exposed by the module and don't worry about internal
> functions?

You can do it with CPP.  Say, if you have a module Foo, with functions
foo, bar and baz, where baz  is not part of the public interface, then
the export list becomes:

    {-# LANGUAGE CPP #-}
    module Foo where (
    , bar
    #ifdef TEST
    , baz

You then run tests with -DTEST.  To make development easier you can add
a .ghci file to your project, with:

    echo ':set -DTEST -isrc -itest' > .ghci

And of course you need to add 

    cpp-options: -DTEST

to your Cabal test-suite section.


[1] http://hackage.haskell.org/package/hspec
[2] https://github.com/hspec/hspec/tree/master/hspec-discover#automatically-discover-and-run-hspec-tests
[3] http://hspec.github.com/

More information about the Haskell-Cafe mailing list