Whereas the previous instalment was concerned with uncovering inconsistencies in the target API, this post is all about testing your code; what you need to test, and how.
I’m a big fan of Test-Driven Development, as those who endured my EECI 2010 talk on the subject will attest.
Unit testing and TDD are huge topics, and I’ve made no attempt to cover them in this post. If you’re completely unfamiliar with such things, the following resources should get you started:
Whatever your chosen testing strategy, it must satisfy the following basic criteria:
Efficient testing is:
Running your tests should not require any preparation time. If your testing strategy requires that your reset a bunch of database tables before each test, or fiddle with configuration files, it is neither quick nor easy.
This is vitally important, because testing that is not quick and easy soon becomes no testing at all.
Every test you write should leave the world as it found it. Just as your tests shouldn’t require any preparation time, so they should not require any recovery time.
If you find yourself recreating rows in the
exp_members table every time you test your
delete_member method, it’s time to rethink your approach.
A couple of keystrokes, or a simple click of the mouse, should be all it takes to run all of your tests. Once again, when it comes to testing, inertia is your mortal enemy.
Your chosen testing strategy should allow you to test edge-cases as easily as common scenarios.
Equally, it should enable you to test “destructive” actions (such as
delete_member) and non-destructive actions (
get_member) with similar ease.
And so, as the hamster of time loses its footing on the exercise wheel of fate and cartwheels helplessly towards the water bottle of destiny, and the seagull of eternity circles the freshly-washed car of hope, it is time to draw this short series of articles to a close.
Before I turn and flounder my way back towards the shoreline, leaving you bobbing helplessly in a sea of unfamiliar APIs, allow me to throw you a lifebelt that may keep you afloat in these treacherous waters: RESTful Web Services is a splendid book covering APIs in general, and RESTful APIs in particular; its only failing being a lack of Humphrey Lyttelton references.