[Twisted-Python] Re: How to write (network) integration tests with Twisted?

Martin Geisler mg at daimi.au.dk
Mon Jan 26 12:00:07 EST 2009

Alessio Pace <alessio.pace at gmail.com> writes:

> I would like to put in my suite of tests some integration tests which
> deal with running some exchange of messages among 2 endpoints (bound
> locally on the same machine on 2 different ports).
> I noticed (and read afterwards..) that I can't run()/stop() the
> reactor multiple times, so I was wondering what's the suggested way to
> deal with this kind of situation.
> Sorry if perhaps it is documented somewhere on the Twisted doc but
> apart from the mention to "Trial" (which I'm not sure responds to my
> needs) I haven't found much.

Trial is what you want, it knows how to deal with the Deferreds you are
likely to return from your tests.

The point is that you can do stuff like this where getPage is a function
which returns a Deferred:

  def test_something(self):
    p = getPage("http://example.com/")
    p.addCallback(self.assertEquals, "<html>...</html>")
    return p

When you return p from the test, the reactor will wait until p has fired
before starting the next test. When p fires, the callbacks are executed
like normal, and here the callsback compares the HTML retrieved with the
expected content. If p does not fire within a given timeout (120 seconds
by default, I think) the reactor will declare the test a failure.

Take a look at the tutorial here:


Use the link for "Original Format" at the bottom to view as HTML, though
I'm getting an XML parse error from my browser when I do this.

I'm using the twisted.protocols.loopback module for connecting several
parties with a pseudo-transport. This allows the testing code to behave
like in your normal application, but the communication is done with
function calls instead of actual network traffic.

Martin Geisler

VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multiparty Computation) to Python. See: http://viff.dk/.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20090126/14f50402/attachment.pgp 

More information about the Twisted-Python mailing list