I read the official tutorial on test-driven development, but it hasn't been very helpful in my case. I've written a small library that makes extensive use of twisted.web.client.Agent
and its subclasses (BrowserLikeRedirectAgent
, for instance), but I've been struggling in adapting the tutorial's code to my own test cases.
I had a look at twisted.web.test.test_web
, but I don't understand how to make all the pieces fit together. For instance, I still have no idea how to get a Protocol
object from an Agent
, as per the official tutorial
Can anybody show me how to write a simple test for some code that relies on Agent to GET
and POST
data? Any additional details or advice is most welcome...
Many thanks!
How about making life simpler (i.e. code more readable) by using
@inlineCallbacks
.In fact, I'd even go as far as to suggest staying away from using
Deferred
s directly, unless absolutely necessary for performance or in a specific use case, and instead always sticking to@inlineCallbacks
—this way you'll keep your code looking like normal code, while benefitting from non-blocking behavior:Trial should take care of the rest (i.e. waiting on the
Deferred
s returned from the test functions (@inlineCallbacks
-wrapped callables also "magically" return aDeferred
—I strongly suggest reading more on@inlineCallbacks
if you're not familiar with it yet).P.S. there's also a Twisted "plugin" for nosetests that enables you to return
Deferred
s from your test functions and have nose wait until they are fired before exiting: http://nose.readthedocs.org/en/latest/api/twistedtools.html