[Haskell-cafe] packages and QuickCheck

Jason Dagit dagit at codersbase.com
Tue Sep 9 20:08:58 EDT 2008


2008/9/9 Conal Elliott <conal at conal.net>:

> Where do you like to place your tests?  In the functionality modules?  A
> parallel structure?  A single Test.hs file somewhere?

The last time I had a chance to experiment with how to do this I used
a single Test.hs for the whole project and I think that is a bad idea
now.

I agree that you don't want to clutter your code with the test cases.
While it's good to have the tests accessible and near to the actual
code it can be distracting too.

The approach I used is here:
http://blog.codersbase.com/2006/09/01/simple-unit-testing-in-haskell/

Basically, I used the H98 parser plus TH to collect all the test cases
together and generate the test harness at compile time.  This meant
that any module that had a test case had to be plain H98 (but again,
only Test.hs had test cases).  On the other hand, specifying tests was
as simple as starting a function name with "prop_".  You could also
modify my technique to look through all modules, or modules with
specific names.

Another approach is to use conditional compilation with something like
CPP.  This could allow you to export everything from modules only
while testing and then you could have Foo.hs and FooTests.hs for every
module.  The latter one would import everything from Foo.hs and define
the tests.

If you don't like conditional compilation, another approach I think is
decent is to have FooPrivate.hs (or FooInternal.hs), which is imported
into Foo.hs and FooTests.hs.  You could set things up so that only
Foo.hs and FooTests.hs are allowed to import FooPrivate.hs.  You could
of course write a script to enforce this, but something that tends to
be simpler and equally effective is just to politely ask that people
do not import FooPrivate.hs except in FooTests.hs and Foo.hs.

I hope that helps,
Jason


More information about the Haskell-Cafe mailing list