TDD, Jython & REST (Part 2)
In part one of this rumination, I discussed test-driven-development of a REST-style application written using Jython. An example of a test method would be something like:
def test_getvaliduser():
req = MockHttpServletRequest('GET', 'user', 'http://localhost:8080/ws/user/testuser1', '', '')
req.setContentType('text/html')
res = MockHttpServletResponse()
user.doGet(req, res)
assert res.getStatus() == 200, 'incorrect status %s (%s)' % (res.getStatus(), res.getReason())
assert res.getContentType().startswith('text/html'), 'expected text/html, got %s' % res.getContentType()
The main problem I have with this idea, is the amount of coding (read: effort) required for each test method, and therefore the amount of duplicated effort testing each resource (servlet). Testing other components in this manner, or testing non-standard functionality, is another matter, of course.
I’ve come up with what I believe is a more workable solution. Reducing the previous example to something like the following:
== Get Valid User
> GET http://localhost:8080/ws/user/testuser1
< assert status 200
Which is a serious reduction in the amount of typing required for a test, particularly when you add the setup functions (missing from the 'old' test example):
== Get Valid User
> PUT http://localhost:8080/ws/user '' \
'username=testuser1&email_address=testuser1@test.com&level=1&password=password'
> GET http://localhost:8080/ws/user/testuser1
< assert status 200
< assert header Content-Type text/xml
> DELETE http://localhost:8080/ws/user/testuser1
Hopefully the script is relatively straightforward to understand: '==' signifies a new test, '>' is an http call, and 'here.
The next step is to add handlers for checking the content in the response (particularly xml), both in form and content.
(If anyone is interested in a working example, let me know and I'll endeavour to post something useable)


