[Haskell-cafe] HSpec vs Doctest for TDD

John Wiegley johnw at newartisans.com
Tue Jun 24 23:36:39 UTC 2014

>>>>> Gautier DI FOLCO <gautier.difolco at gmail.com> writes:

> I'm a huge fan of TDD (Test-Driven Development) et I used to use tools such
> as RSpec (Ruby). So naturally, I looked to HSpec, but it seems not idiomatic
> in Haskell.

> I have a bunch of questions:
> - Do you do TDD?

Not as such, no, although there are times when I write what I want to say
first, and then implement how to say it.  This isn't my primary mode of
development, however.

> - Which tools do you use?

Doctest, hspec+hunit, quickcheck, fuzzcheck.

> - Is doctest "better" (in some senses) than HSpec? Why?

I think they address different needs, actually.

> - Are HSpec and Doctest complementary? If so, in which case do you use one
> or another?

I use doctest as much as possible, not only to create a test, but to
demonstrate and motivate use of a function for anyone reading the docs.  Hspec
can handle more complicated tests that would be too heavyweight in that

> - Is there some Haskell-specific good practices do to TDD?

First of all, invest in your types.  As much as you can, avoid naked tuples,
Strings or Ints.  Use newtype and create type wrappers.  Build new ADTs.
Encode your invariants so that many of your "tests" become unnecessary.

Then, when you have effects whose interplay cannot be encoded in types, focus
on testing the possible runtime combinations.  But even then, think about how
you could encode that invariant in a type.


More information about the Haskell-Cafe mailing list