For the last few jobs I’ve had, Service Oriented Architecture has been a strong theme. Having services presenting REST apis that consume and produce XML has been the regular approach. When it comes to testing consumers of those services, I have found Python to be an extremely useful and convenient tool. It’s quick and easy to throw an HTTP server up in Python, and hard code the values that it returns. The problem is though, that I have ended up just adding code to the do_GET / do_POST methods as I add tests that handle different expectations. This ends up separating the expectations and return values from the test, and making maintaining the stub code harder than it should be.
To resolve this, I’d like to add expectations of the stub http server in the same way as with defining mocks. The syntax would look something like this
-
server.expect(method="GET", url="/address/\d+$").
-
and_return(mime="text/xml", file_content="path/to/response.xml")
-
capture = {}
-
server.expect(method="PUT", url="/address/\d+$", data_capture=capture).
-
and_return(reply_code=201)
-
server.expect(method="POST", url="address/\d+/inhabitant", data=‘<inhabitant name="Chris"/>’).
-
and_return(reply_code=204)
-
server.expect(method="GET", url="/monitor/server_status$").
-
and_return(content="<html><body>Server is up</body></html>", mime_type="text/html")
-
# execute system under test
-
self.assertEquals("<expected><xml/></expected>", capture["body"])
-
So, over the next couple of weeks I’ll be putting this together, which should among other things allow me to improve the end to end testing story for djangoRESTModel.

Hey…..nice post!!
Awesome, No more words to explain
just….cool blog.