[twill] Fwd: Testing a web API with twill?

Mark Ramm mark.mchristensen at gmail.com
Mon Apr 9 20:52:19 PDT 2007

Oops, I meant to send this to the list too.

--Mark Ramm

---------- Forwarded message ----------
From: Mark Ramm <mark.mchristensen at gmail.com>
Date: Apr 9, 2007 11:02 PM
Subject: Re: [twill] Testing a web API with twill?
To: Ramon Felciano <felciano at ingenuity.com>

> I'm trying to find a web unit and load testing tool that will let me test
> URL-based APIs. This is basically a parameterized URL that will return
> JSON-encoded results.

This is something I've done fairly extensively over the last couple of
weeks, using the python API that twill provides.

>I would like to be able to easily set up 100+ test
> cases that are all test the same API, but with different URL parameter
> values and different expected results. I would to be able to flexibly
> configure these test cases, ideally as a text table or other
> simple-to-create-and-parse data structure, and generate a report of
> pass/fail cases for each test.

You could use a CSV file or a simple SQLite database for your test data.

> I would really like to use something like twill, but I'm getting stuck on 2
> fronts:
> 1.      How to configure a single test case. The key here is that to check
> for valid results I need to unserialize the json returned result.

The key her is to use the python version of twill.  I did all this
with standard test functins which are discovered and run with Nose.
To do this you write something like:

from twill.commands import *
import simplejson

def test_something()
  assert ---something----

Where something is whatever you want to test about the data you get
back from the server.

> 2.      How to configure the series of test cases.

You can use Nose to generate a large number of data driven tests


look for the section entitled Test Generators.

Also, urllib is your friend when creating HTTP post URLs dynamically
from data (you'll likely need to encode some stuff at various points).

We've also hacked together some basic support for creating arbitrary
HTTP Post parameters within Twill to simulate complex posts that would
be done by JavaScript.  Hopefully we'll get permission to release that
code, but if not and you need something like that I can give you some
pointers on how to get started.

Mark Ramm-Christensen
email: mark at compoundthinking dot com
blog: www.compoundthinking.com/blog

Mark Ramm-Christensen
email: mark at compoundthinking dot com
blog: www.compoundthinking.com/blog

More information about the twill mailing list