[twill] Testing a web API with twill?

Stephen Thorne stephen at netboxblue.com
Mon Apr 9 21:03:01 PDT 2007


On 2007-04-09, Titus Brown wrote:
> -> 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. 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. 
> -> 
> -> 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.
> -> Twill obviously doesn't have this built in, so I assume I will have to
> -> call a twill script from python and then record its results for
> -> subsequent processing. Is that right? Should the calling code itself
> -> then be part of a unit testing framework?
> -> 2.	How to configure the series of test cases. I would like to avoid
> -> writing a separate test class / function for each case, and instead
> -> somehow iterate over the param/result pairs. I can't figure out how to
> -> do this without having the entire suite abort after the first test
> -> failure. I would like to run all 100 tests, and have it report back
> -> which ones succeeded and which ones failed.
> 
> Hey Ramon,
> 
> I think a combination of twill and nose would work quite well.

nose is very similar to py.test, which I have had great success writing
unit tests with.

Here's some blogging I did on the subject some time ago:
http://shiny.thorne.id.au/2006/04/twill-and-pytest.html

Also, here's a quick example of how to go about dealing with the contents of a
page directly, this is using py.test:

from twill import commands

def setup_module(mod):
    commands.go('http://example.com/')
    utils.gohost(TEST_HOST)

class TestSetOption(object):
    def test_get_primary_email(self):
        commands.go('/api/users/get_option?option=PRIMARY_EMAIL_ADDR'
                '&username=mustexist')
        commands.code('200')
        self.compare_csv([['value'], ['foo at bar.com']])

    def compare_csv(self, expected):
        body = commands.get_browser().get_html()
        data = list(csv.reader(StringIO(body), csv.excel_tab))
        assert data == expected

py.test is available as part of py-lib from codespeak.net. I'm not sure
if they have a 'stable' release, I run it from svn... It's similar to
nose anyway, and the same concepts apply.

-- 
Regards,
Stephen Thorne
Development Engineer

Scanned by the NetBox from NetBox Blue
(http://netboxblue.com/)




More information about the twill mailing list