[Twisted-Python] Testing with trial, adbapi questions

Christian Simms christian.simms at gmail.com
Mon Jul 23 11:00:54 EDT 2007

On 7/20/07, Brendon Colby <brendoncolby at gmail.com> wrote:
> On 6/26/07, Christian Simms <christian.simms at gmail.com> wrote:
> >
> > Looks like I mis-assumed your problem, sorry.  In the setUp method in
> > your tests, you should return a Deferred which fires when all your data is
> > loaded.  This can be a pain if you already organized your code into classes
> > inheriting from twisted.application.service.Service which have
> > startService/stopService calls. It's a pain because trial doesn't create an
> > application object like running inside twistd does.  So, in the case of
> > writing unit tests for applications, you can abstract out your
> > initialization code into a method, and then call that method directly in
> > your setUp method.
> I've spent some time working on this, and I see the logic in doing so
> (firing a deferred when my data is loaded) but I'm stuck at the point of how
> to do this. In my factory, I call this method in __init__ to load data:
>   def loadDevelopers(self):
>     def cb(results):
>       for result in results:
>         self.addDeveloper
> (Developer(result['auth_host'],result['auth_url'],
>           result['devid'],self,result['key'],result['post_host'],
>           result['post_url']))
>     return self.dbPool.runQuery("select * from developers where
> active=1").\
>       addCallback(cb).addErrback(printError)
> I created a tac file and am using twistd to run this and it works great.
> But in my unit tests, what happens is my tests run before my data is loaded
> (and fail). I haven't been able to figure out how to tell my tests to run
> after this data is loaded. Here's my setUp method:
>     def setUp(self):
>         self.file = StringIOWithoutClosing()
>         self.transport = FileWrapperThatWorks(self.file)
>         self.protocol = sserver.SServerProtocol()
>         self.protocol.factory = sserver.SServerFactory(self.protocol)
>         self.protocol.factory.dbPool.start()
>         self.protocol.makeConnection(self.transport)
>     def testSendDeveloperID(self):
>         print self.protocol.factory.developers # This dictionary prints as
> empty - it shouldn't!
> I guess I'm not making the connection here. How do I code the deferred in
> setUp to fire when my data is loaded? Also, if I abstract out this logic to
> an initialization method, how would I have it fire properly when I run under
> twistd? Here's my tac file:
> factory = SServerFactory(protocol=SServerProtocol)
> application = service.Application("SServer")
> sserverService = internet.TCPServer(4000,factory)
> sserverService.setServiceParent(application)
> I appreciate the help!
> Brendon
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
To fix your unit tests to "wait" until your data is loaded by your
loadDevelopers method, you need to have your setUp method return the
Deferred created by loadDevelopers's. There are a bunch of ways to do this,
the simplest is probably to have your factory's __init__ method store the
Deferred created by loadDevelopers into a member variable (say self.deferred),
and then add a new line to the end of your setUp method above:

           return self.deferred

This way, each unit test will not start until the Deferred from
loadDevelopers fires.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20070723/b919016d/attachment.htm 

More information about the Twisted-Python mailing list