[Twisted-Python] Testing Twisted code without trial

Adi Roiban adi at roiban.ro
Tue Jan 22 04:29:15 EST 2013

On 22 January 2013 02:21,  <exarkun at twistedmatrix.com> wrote:
> On 20 Jan, 02:35 pm, adi at roiban.ro wrote:
>>I start my pet project using Python Nose as test runner. At the start,
>>Twisted not used at all.
>>Later I discovered Twisted and start using it... but testing Twisted
>>requires trial.
>>I resist migrating to Trial and ended up with a custom code for
>>running Twisted tests together with Nose:
>>I though I can share this code on the list, in case there is someone
>>else wrestling with deferres testing outside of Trial.
>>Any feedback or comments is much appreciated.
> The implementation is somewhat saddening in its use of Twisted internals
> (which makes it fragile and subject to easy breakage by new Twisted
> releases) and limited platform support (it won't work on Windows,
> afaik).
> You may also find problems with direct use of `reactor.iterate`, since
> use of this API is highly discouraged due to re-entrancy issues and its
> implementation is not all that well tested by Twisted's own test suite.
> If it works for you, great, but I would hesitate to recommend it to new
> projects as a testing tool.  As Itamar suggests, nose should be able to
> run xUnit-style tests.
> Jean-Paul

Thanks for your feedback.

This code also works on Windows and on other Unix systems (OSX, Solaris)


The new methods successResultOf and failureResultOf looks good, but I
think they only work with deferred that already have a result.


I am aware that the whole thing is fragile... and I am aware of the
consequences of using internal non-public methods.

I checked the code from twisted.trial.unittest.TestCase but I could
not spot how it waits for deferreds execution.

What part of twisted.trial.unittest.TestCase executes and waits for
the deferred?

Can I use that code to implement something similar to successResultOf
and failureResultOf but which also executes the deferred?

Adi Roiban

More information about the Twisted-Python mailing list