[Haskell-cafe] HSpec vs Doctest for TDD

Michael Orlitzky michael at orlitzky.com
Tue Jun 24 21:20:41 UTC 2014

On 06/24/2014 04:55 PM, Gautier DI FOLCO wrote:
> Hi all,
> 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?
> - Which tools do you use?
> - Is doctest "better" (in some senses) than HSpec? Why?
> - Are HSpec and Doctest complementary? If so, in which case do you use
> one or another?
> - Is there some Haskell-specific good practices do to TDD?
> Thanks in advance for your lights.

I like to use doctests for examples because they double as
documentation. So if you have a function that's supposed to lowercase a
string, you might show,

--  Caps get lowercased:
--  >>> lowercase_it "HELLO"
--  "hello"
--  And lowercase letters are left alone:
--  >>> lowercase_it "hello"
--  "hello"
--  Numbers are unaffected:
--  >>> lowercase_it "123"
--  "123"
--  It doesn't crash on the empty string:
--  >>> lowercase_it ""
--  ""

If you think it would be useful to the reader, make it a doctest. For
tests with a big setup/teardown -- like 20 lines of inserting junk into
the database -- I use HUnit instead.

For properties like idempotence, there's QuickCheck.

I usually start out with doctest, because everything needs an example.
Then if complicated tests or properties arise, I'll create a tasty[1]
suite with HUnit[2]/QuickCheck[3] tests. Tasty basically groups all of
your HUnit, QuickCheck, SmallCheck, etc. tests into one big suite with
nicely-formatted output.

HSpec is redundant once you have all of that in place, so I personally
haven't found a niche for it yet.

[1] http://hackage.haskell.org/package/tasty
[2] http://hackage.haskell.org/package/tasty-hunit
[3] http://hackage.haskell.org/package/tasty-quickcheck

More information about the Haskell-Cafe mailing list