9

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.

Sean Clark Hess
  • 15,859
  • 12
  • 52
  • 100
  • 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 Feb 26 '12 at 00:58
  • Out of curiosity, what is your use-case that you're writing another JSON library rather than using an existing one? – ivanm Feb 26 '12 at 09:46
  • Why is it IO heavy? Are you sure that it can't be refactored so that more of it is pure? – dan_waterworth Feb 26 '12 at 11:36
  • I'm not writing another JSON library. I'm writing a web application that produces and consumes JSON objects. – Sean Clark Hess Feb 28 '12 at 17:40
  • 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 Feb 28 '12 at 17:41

2 Answers2

3

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.)

Community
  • 1
  • 1
Tikhon Jelvis
  • 67,485
  • 18
  • 177
  • 214
  • I got it working similarly to this: https://gist.github.com/967505, but it hits the api call 100x, which seems like overkill. Any way to dial that down? Should I? – Sean Clark Hess Feb 28 '12 at 17:43
  • 1
    You can use something like `quickCheckWith stdArgs { maxSuccess = 25 } someProp` to run a test with 25 arguments instead of 100. – Tikhon Jelvis Feb 28 '12 at 18:46
1

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

fujimura
  • 11
  • 1