Django unit testing api logs

Thu Aug 13, 2009

The last several years I’ve designed a few APIs. Designing them is fun, implementing them is fun, testing them is not as fun. Partly this is because it’s hard to guess how someone is going to misinterpret what you want them to do.

I hit upon a better way to do testing and debugging that hadn’t occurred to me until recently. First, you have to log all the calls to your API. This also helps when you want to do performance analysis of your api later, because you can have the traffic come in according to the timestamps at the correct latencies. When you record the call to your database, make sure to record all the parameters, so you can recreate the call later.

Second, make sure you record whether the call was successful or not. Some of you may only want to record failures, because you’re not interested in successful calls. You’ll then want to create a django fixture of the calls you want to test.

Create a django unittest which loads the fixture, runs the tests, and voila you can find and fix your errors. You don’t have to spend a lot of time with artificial testing, because you are using real data.