[Twisted-Python] the right way of unit testing protocols

Itamar Shtull-Trauring itamar at itamarst.org
Wed Jul 30 15:10:39 EDT 2003

On Thu, 31 Jul 2003 00:26:07 +0530
Jeethu Rao <jeethur at sancharnet.in> wrote:

> What is the right way of unit testing protocols in twisted ?
> Currently, I'm just subclassing my protocol with unittest.TestCase
> And directly calling the dataRecieved with a string and am checking
> If the overridden callbacks (like dataRecieved) are called.

That is indeed a good way to test.

> The tests pass, but back when I was writing threaded servers,
> I would actually setup a server on localhost and would connect to
> The server from another thread, for unit testing. That gives me a lot
> more confidence.

And doing this sort of testing in addition is also a good idea.

> I know that I can setup two reactors, for the client and the server
> and then try to make them talk, But I'm sure there's a shortcut.
> I just could'nt figure out the test suites from the Twisted sources.

You only need one reactor. (And twisted doesn't support multiple
reactors at the moment anyway.) So don't look for that in the code. Just
look at how the tests are done.

In general you'd just do some variant on:

 reactor.listenTCP(1234, myServerFactory)
 reactor.connectTCP("", 1234, myClientFactory):
 while someConditionIsn'tSet:
 # at this point some exchange should have finished successfully

Itamar Shtull-Trauring    http://itamarst.org/
http://www.zoteca.com -- Python & Twisted consulting

More information about the Twisted-Python mailing list