Perspectives on learning and using Haskell

Duncan Coutts duncan at coutts.uklinux.net
Sun Jan 4 15:47:25 EST 2004


On Sun, 2004-01-04 at 10:20, Graham Klyne wrote:
> Which leads to a question:  I've been thinking that the "white box" tests 
> may be better served by test expressions coded *within* the module 
> concerned.  In many cases, I create these, then en-comment them when the 
> code is working.  I would expect that when using GHC to compile a 
> stand-alone Haskell program, any expressions that are not referenced are 
> not included in the final object program, so leaving these test cases 
> uncommented would be harmless:  is this so?

If your test functions are not exported, I would expect that this is the
case.

I normally include test code in the same module. That way, I'm more
likely to notice when I change/break the interface, as the test code
then fails to compile. Then I can load each module up in ghci/hugs and
run the tests. If the tests are sufficiently automated I stick in a main
function which runs all the tests.

Jon Fairbairn mentioned that it would be nice to automatically run the
tests every time the module was compiled. You could do that with
Template Haskell if the staging restrictions were relaxed a bit; at the
moment you are not allowed to call functions defined in the same module.

On a side note, I've found QuickCheck to be great for these kinds of
unit tests. As an example, I was able to turn someone else's code for a
trie data structure into a multi-trie (like a bag is to a set) without
fully understanding the code and still be fairly confident that the code
was correct! I caught a couple subtle bugs that I would never have found
otherwise. I guess it worked well in that case because the properties of
the data structure were few and easy to describe.

Duncan



More information about the Haskell-Cafe mailing list