[Twisted-Python] Stopping Reactor in UnitTest Callback

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Thu Jan 10 13:51:34 MST 2013


On 07:58 pm, fabian.rothfuchs at googlemail.com wrote:
>Hey there,
>
>
>I'm trying to start and stop a twisted server in a unittest:
>http://dpaste.org/qwaUt/ .

Why?  This isn't the typical way to write unit tests for Twisted-based 
code.  The reactor is started and stopped by trial as necessary.  Your 
code should just expect that it is running and let trial stop it when it 
is time to stop.

If you have asynchronous code to test, you return a Deferred from a test 
method and the test completes when the Deferred fires.  Not when the 
reactor stops.

Also, a lot of people have come to dislike this style of test, instead 
preferring to use test doubles that don't involve real I/O or time so 
that they can completely test the logic of their code deterministically 
- thus also without ever running a real reactor or needing to use the 
"return a Deferred from a test method" feature of trial.

Jean-Paul




More information about the Twisted-Python mailing list