9
votes

I'm writing a Haskell JSON API, and I'd like to write some tests. The thing is really IO heavy, so I think it makes the most sense to write some functional tests: (Add a Foo, see if /foos/ returns it), etc.

I've read about QuickCheck of course, but it seems to focus on testing pure code.

How should I write/run functional tests that need to test an API? If it matters, I'm using Scotty/WAI. Although an example would be great, a good couple links and some advice would be fine.

2
Related question: What's the simplest HTTP client library? As in, handles PUT/DELETE, easy, sets the Content-Length header for you? - Sean Clark Hess
Out of curiosity, what is your use-case that you're writing another JSON library rather than using an existing one? - ivanm
Why is it IO heavy? Are you sure that it can't be refactored so that more of it is pure? - dan_waterworth
I'm not writing another JSON library. I'm writing a web application that produces and consumes JSON objects. - Sean Clark Hess
It is IO heavy because it's a web application :) It primarily grabs some data from a request, updates or reads a database, and spits something back out. You're right, I could test the pure bits, but most of my errors are in the IO steps, so I thought a functional test would be good. - Sean Clark Hess

2 Answers

3
votes

I've never used it, but QuickCheck actually does support testing monadic code. Look at Testing IO actions with Monadic QuickCheck for more information. If your api has some invariants that are easy to express, this is probably a good way to test it. (In my experience with pure code, QuickCheck covers more corner cases than I can think of, which makes it very useful.)

1
votes

I have a half-baked(yet almost done) blog entry to explain how to test wai application with hspec. Hope it helps!

https://github.com/fujimura/wai-hspec-example/blob/master/testing-wai-app-with-hspec.md https://github.com/fujimura/wai-hspec-example