[Twisted-Python] how to unittest the deferred

Jp Calderone exarkun at divmod.com
Wed Sep 28 21:35:11 EDT 2005

On Thu, 29 Sep 2005 10:28:31 +0900, Hyungyong Kim <yong27 at gmail.com> wrote:
>Hi all,
>I'm newbie of Twisted. It's my first posting to this mail list. I'm
>sorry for my stupid question.
>Because I'm not familiar with asnyc programming, I'm suffering with
>deferred. So I want to make unittest to check result easily as follow.
>from twisted.trial import unittest
>from twisted.internet import defer, reactor
>def someFunction():
>    d = defer.Deferred()
>    d.addCallback(gotValue)
>    return d
>def gotValue(myExpect):
>    return "result"
>class SomeTest(unittest.TestCase):
>    def test1(self):
>        self.assertEquals('result', unittest.deferredResult(someFunction()))
>But this test is not working. How can I check the "result" value.
>Thanks for your kind support.

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


More information about the Twisted-Python mailing list