[Twisted-Python] how to unittest the deferred
![](https://secure.gravatar.com/avatar/3b16dac3ae30bfabb12577e5ac8d8656.jpg?s=120&d=mm&r=g)
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. Hyungyong
![](https://secure.gravatar.com/avatar/7ed9784cbb1ba1ef75454034b3a8e6a1.jpg?s=120&d=mm&r=g)
On Thu, 29 Sep 2005 10:28:31 +0900, Hyungyong Kim <yong27@gmail.com> wrote:
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
![](https://secure.gravatar.com/avatar/3b16dac3ae30bfabb12577e5ac8d8656.jpg?s=120&d=mm&r=g)
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@divmod.com>:
How can I test that. In my real code, someFunction returns deferred from DB connection.
![](https://secure.gravatar.com/avatar/1327ce755b24b956995d68accae3eab2.jpg?s=120&d=mm&r=g)
On 9/29/05, Hyungyong Kim <yong27@gmail.com> wrote:
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 of like: self.assertEquals(thingReturnedEventuallyBySomeFunction, 'result') 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: class DBTest(unittest.TestCase): def testDbThing(self): d = getDBConnection() d.addCallback(self._cb_testDbThing) return d def _cb_testDbThing(self, connection): d = connection.doSomething() d.addCallback(self.assertEquals, 'expectedValue') return d I hope this helps, jml
![](https://secure.gravatar.com/avatar/7ed9784cbb1ba1ef75454034b3a8e6a1.jpg?s=120&d=mm&r=g)
On Thu, 29 Sep 2005 10:28:31 +0900, Hyungyong Kim <yong27@gmail.com> wrote:
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
![](https://secure.gravatar.com/avatar/3b16dac3ae30bfabb12577e5ac8d8656.jpg?s=120&d=mm&r=g)
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@divmod.com>:
How can I test that. In my real code, someFunction returns deferred from DB connection.
![](https://secure.gravatar.com/avatar/1327ce755b24b956995d68accae3eab2.jpg?s=120&d=mm&r=g)
On 9/29/05, Hyungyong Kim <yong27@gmail.com> wrote:
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 of like: self.assertEquals(thingReturnedEventuallyBySomeFunction, 'result') 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: class DBTest(unittest.TestCase): def testDbThing(self): d = getDBConnection() d.addCallback(self._cb_testDbThing) return d def _cb_testDbThing(self, connection): d = connection.doSomething() d.addCallback(self.assertEquals, 'expectedValue') return d I hope this helps, jml
participants (4)
-
Hyungyong Kim
-
Jonathan Lange
-
Jp Calderone
-
Mary Gardiner