Thursday, April 05, 2007

Unit testing in Haskell

Test-Driven-Development is a programming technique where you write small programs (test cases) in advance of coding the actual piece of software you want to write. If done well, this helps to clarify the various combinations of input/output/environment for your code, and makes it easier to implement it. As a nice extra, you speed up testing - all you have to do is to run all those test cases again, and they will point out whether everything still works as expected.

So far so good. (And if you're coming from Java development, you know that song by heart anyway ;-). But can we do this in Haskell? Sure. There are actually two different well-supported approaches in Haskell to do test-driven development. One is more oriented towards functional programming, focusing on declaring properties of functions - this one is supported by QuickCheck (and not covered in this post); the other is aligned with the 'classical' JUnit tool. There is an implementation of this approach in the form of HUnit. I have written a tutorial that gives an introduction to unit testing with HUnit.

Again, I would have have preferred to post the full text here, but that is too inconvenient with all the code listings. But you are welcome to comment on the tutorial here on this post.


Dystopian said...

I'm new to haskell. Just setup Eclipse with haskell and now I'm thinking about how to order my code? How do you order your haskell code in eclipse? And how do you seperate test code and production code?

Leif Frenzel said...

Well, welcome and thanks for asking :-)

There isn't an 'official way' prescribed by EclipseFP where to put which sorts of files, but we make a few assumptions:

Much EclipseFP functionality (such as autocompile) requires that your sources are in a Haskell project, and they must be in the source folder of that Haskell project. You create a Haskell project with New > Project > Haskell Project.

Normally, a source folder etc. is created by the wizard. It doesn't have to be named 'src', that's only the default name. You can change the default name for the source folder that the wizard creates in the Preferences.

There is no special 'test' folder for unit tests. (But it would be a nice idea to have the wizard optionally create one, and then we could run autocompile also over that folder. Could be a feature for later versions.)

Autocompile runs automatically something like ghc --make over files that have changed when you save them. From the compiler run, we take the errors and show them in the source file and in the Problems View. As I said, this happens only for files in source folders.

You can use the Haskell Editor and Outline on all .hs files and .lhs files (not only on those which are in a source folder).

There is currently no special support for test runners (i.e. no equivalent to JDTs JUnit runner and JUnit View). So you have to invoke your test cases yourself. Actually there are some different ways how you could do that. At least I myself have over time figured out a few practices. (Nothing spectacular, mostly workarounds around missing EclipseFP features, I admit :-( I think they might be of broader interest, so I think I'll write them up and make a top-level post here (over the next few days).

You might also find a little more info in the users guide that comes with EclipseFP (Help > Help Contents > Functional Programming > Haskell).

Hope this helps, feel free to ask if you have more questions (there's also the EclipseFP mailing list, you can ask there too).

Thanks && ciao,

Robban said...

Ok thanks. I'm looking forward to that post.

Anonymous said...
This comment has been removed by a blog administrator.