[Twisted-Python] Global reactor unit tests in the Twisted test suite

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Tue Nov 1 17:46:51 EDT 2011

On 09:37 pm, radix at twistedmatrix.com wrote:
>On Tue, Nov 1, 2011 at 5:29 PM, Anton Gyllenberg <anton at iki.fi> wrote:
>>On Tue, Nov 1, 2011 at 20:48, Glyph <glyph at twistedmatrix.com> wrote:
>> > On Nov 1, 2011, at 12:14 PM, Phil Mayers wrote:
>> >> I find the "pass reactor as 1st argument to everything" API pattern
>> >> messy. I'm sure there's a good reason. What is it?
>> >
>> > This pattern is a solution to the problem, but I agree that it is
>>possibly not the optimal solution.  It sort of points in a direction 
>>every possible module that might be imported becomes an argument to 
>>function.  After all, there are plenty of other modules which have to 
>>mocked for testing, why not just make everyone's __init__ method take
>>sys.modules as an argument too, and never import anything?  In more 
>>systems this can definitely turn into a bit of a mess.
>>I've often wondered whether there is a *real* use of the reactor
>>argument other than unit testing. I haven't had other uses for it, and
>>haven't seen (or perhaps not understood) real application code making
>>use of it. Now, I've sometimes made an argument for unit testing,
>>other than the obvious quality assurance, that unit tested code will
>>be more reusable and have better interfaces as it already is used for
>>at least two things: the real application and the unit test. However,
>>if accommodating unit testing requires sacrificing the natural
>>interface, then that kind of takes the edge off that argument.
>>Thanks to everybody for the discussion and input. Very informative!
>Here's the thing: not only does removing the global reactor make *our* 
>tests a lot nicer, it makes the unit tests of our users much easier to
>write, since they will want to use our APIs in their unit tests in a 
>that doesn't require ugly global hacking. I don't know how many times I
>have raged at some crappy library whose code is incompatible with 
>unit testing. Having testable interfaces is a really valuable feature 
>can provide to our users.

To emphasize, having testable interfaces is a *really* *really* valuable 
feature we can provide to our users.  If you're a Twisted user and 
you're not taking advantage of it, you're missing out on a lot.


More information about the Twisted-Python mailing list