Unit testing

17 views
Skip to first unread message

Jonathan Tang

unread,
Feb 6, 2007, 9:17:04 AM2/6/07
to web.py
Hi all,

I was wondering what folks use for unit testing web.py apps. I'd like
something where I could just run a few SQL scripts to restore the
database to a consistent state, simulate an HTTP GET or POST, and then
run some assertions on the returned result. Something like:

empty_db = ['schema.sql']
sample_users = empty_db + ['users.sql']
web.test.load_db(sample_users);

web.test.post('/my_app/form', field1='me', field2='myvalue',
field3='afile')
web.test.assert_redirect(302, '/my_app/results')
web.test.assert_db_contains('user_roles', db_field1='me',
db_field2='myvalue')
web.test.assert_cookie('last_val', 'myvalue')

web.test.get('/my_app/results')
web.test.assert_contains('Thank you for submitting your form.')
web.test.assert_regexp('(<html>)?myvalue')

Is there anything like this already in existence, or any easy way to
adapt present Python testing frameworks (unittest etc.) to do this?
I'm not averse to hacking something up, but I want to make sure
there's nothing already in existence that does what I want.

Regards,
Jonathan

Gary Bernhardt

unread,
Feb 6, 2007, 10:42:21 AM2/6/07
to we...@googlegroups.com
On 2/6/07, Jonathan Tang <nostra...@gmail.com> wrote:

Hi all,

I was wondering what folks use for unit testing web.py apps.  I'd like
something where I could just run a few SQL scripts to restore the
database to a consistent state, simulate an HTTP GET or POST, and then
run some assertions on the returned result.  Something like:

I use twill (http://twill.idyll.org/) to test my web apps.  It's a web browser with a programmatic interface.  For your example, you could say

go('/my_app/form') # navigate to an absolute URL
formvalue(1, 'field1', 'me') # populate the form fields
formvalue(1, 'field2', 'myvalue')
formvalue(1, 'field3', 'afile')
submit()
url('/my_app/results') # assert that we got redirected
find('Thank you for submitting your form.')
find('<html>.*me') # find uses regexes
find('<html>.*myvalue')

Twill also has a command line mode, so you can use it as an interactive browser to design your tests.  It's a fairly complete web browser as well - it will follow redirects automatically, handle cookies correctly, etc.
Reply all
Reply to author
Forward
0 new messages