[Twisted-Python] how to unittest the deferred
jml at mumak.net
Wed Sep 28 22:12:23 EDT 2005
On 9/29/05, Hyungyong Kim <yong27 at gmail.com> wrote:
> Thanks for your answer.
> But, in this case, any values are OK. Try to test that after replace
> 'result' to 'aaa'. I'm testing it using "trial" command.
> 2005/9/29, Jp Calderone <exarkun at divmod.com>:
> > I'm not entirely certain why the above doesn't work, although I could make some good guesses. Here's the preferred way to write such a test:
> > class SomeTest(unittest.TestCase):
> > def test1(self):
> > d = someFunction()
> > d.addCallack(self.assertEquals, 'result')
> > return d
> > Jp
> How can I test that. In my real code, someFunction returns deferred
> from DB connection.
I don't really understand your question. I'll explain what Trial does
in the hope that I answer it by accident.
If your test returns a Deferred (as in SomeTests.test1), Trial will
wait until that Deferred has fired before proceeding to the next test.
In the example 'test1', someFunction() returns a deferred. After the
deferred fires (most likely after the connection is made to the
database), self.assertEquals will get called (by your Deferred), kind
If the assertion fails, Trial will record it as a failure. Otherwise
it will be marked as a success.
If you want to connect to the database, *then* do some testing on
something else, here's how it might look:
d = getDBConnection()
def _cb_testDbThing(self, connection):
d = connection.doSomething()
I hope this helps,
More information about the Twisted-Python