[Twisted-Python] unit testing question: twisted with tk
I have a bit of code that combines twisted with Tkinter and now I'd like to write some unittests for it. I can't seem to figure out how to get TwistedTrial to handle this case. I've written a unit test like this: from twisted.trial import unittest from twisted.internet.defer import Deferred import twisted.internet.tksupport root = Tkinter.Tk() twisted.internet.tksupport.install(root) from twisted.internet import reactor class TestTkSocket(unittest.TestCase): def test...(...): .... the one test so far returns a deferred and calls errback on that deferred if the test fails, and callback if it succeeds. When I run the unit test with trial I get this error error: twisted.trial.util.DirtyReactorAggregateError I have an identical unit test for a pure-twisted version of my communication code (no Tk) and it works fine. I am guessing the problem is cleanly getting rid of Tkinter's root at the end of the test and disentangling it from the reactor, but I have no idea how to do that. Any suggestions on how to proceed (e.g. a way to get TwistedTrial to work, or a good alternative)? -- Russell
On 07/18/2012 01:01 PM, Russell E. Owen wrote:
I have a bit of code that combines twisted with Tkinter and now I'd like to write some unittests for it.
I can't seem to figure out how to get TwistedTrial to handle this case. I've written a unit test like this:
from twisted.trial import unittest from twisted.internet.defer import Deferred import twisted.internet.tksupport root = Tkinter.Tk() twisted.internet.tksupport.install(root) from twisted.internet import reactor
class TestTkSocket(unittest.TestCase): def test...(...): ....
the one test so far returns a deferred and calls errback on that deferred if the test fails, and callback if it succeeds. When I run the unit test with trial I get this error error: twisted.trial.util.DirtyReactorAggregateError
The problem is that setting up tk support involves a repeatedly scheduled event, which means it's in the reactor causing that warning. Make sure you call tksupport.uninstall() at the end of each test, by either putting in tearDown or doing self.addCleanup(tksupport.uninstall).
In article <5006FC68.8050307@itamarst.org>, Itamar Turner-Trauring <itamar@itamarst.org> wrote:
On 07/18/2012 01:01 PM, Russell E. Owen wrote:
I have a bit of code that combines twisted with Tkinter and now I'd like to write some unittests for it.
I can't seem to figure out how to get TwistedTrial to handle this case. I've written a unit test like this:
from twisted.trial import unittest from twisted.internet.defer import Deferred import twisted.internet.tksupport root = Tkinter.Tk() twisted.internet.tksupport.install(root) from twisted.internet import reactor
class TestTkSocket(unittest.TestCase): def test...(...): ....
the one test so far returns a deferred and calls errback on that deferred if the test fails, and callback if it succeeds. When I run the unit test with trial I get this error error: twisted.trial.util.DirtyReactorAggregateError
The problem is that setting up tk support involves a repeatedly scheduled event, which means it's in the reactor causing that warning. Make sure you call tksupport.uninstall() at the end of each test, by either putting in tearDown or doing self.addCleanup(tksupport.uninstall).
I put tksupport.install(root) in the setUp and tksupport.uninstall() in the tearDown and it worked perfectly. Thank you very much. I'm thrilled to be able to run these unit tests. -- Russell
participants (3)
-
Itamar Turner-Trauring -
Phil Mayers -
Russell E. Owen