Last Updated: February 25, 2016
·
1.019K
· basicallydan

Use TDD for HTTP API Development

Seriously. It's a heck of a lot easier to use something like API Easy or even just rolling your own simple JSON API test framework with vows or mocha or whatever - than it is to write an API, then go into the command line and call curl after curl, and meticulously checking the response to make sure it includes everything that you expect it to.

Some things to think about while doing this:

  • If you're writing an API using the same language as your API test framework you can probably create objects, store their values for IDs or whatever somewhere and use those values in your tests. This is particularly easy with mongoose in nodejs.
  • If you are using HATEOAS, don't even bother writing request paths in the test setup: just get the test path directly from a former test - that way you're also testing the linked nature of your REST API.
  • Re-use your test components. It's likely, especially with CRUD features, that you'll be testing similar structures of responses frequently.
  • Try setting up a hook maybe with grunt or something similar to run tests every time you save a certain set of files.