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

Anton Gyllenberg anton at iki.fi
Tue Nov 1 17:29:31 EDT 2011

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 where every possible module that might be imported becomes an argument to your function.  After all, there are plenty of other modules which have to be mocked for testing, why not just make everyone's __init__ method take sys.modules as an argument too, and never import anything?  In more complex 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!

-- Anton

More information about the Twisted-Python mailing list