Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing with a twisted deferred

I have been struggling to wrap my head around testing twisted code with deferred's and deferred's in general.

I have a simple test that really should fail, I think.

From my undertstanding in Twisted's test tool Trial, I should be able to return a deferred and when it is finished the test will finish.

Here is my tidbit of code, maybe someone can help.

import unittest, time

from twisted.internet import reactor, defer

class MyTest(unittest.TestCase):

def test_simple_deferred(self):

    print "\nStarting Test"

    my_deferred = defer.Deferred()

    def print_time():
      print time.time()
      self.assertTrue(False)

    my_deferred.addCallback(print_time)

    reactor.callLater(3, my_deferred.callback)

    print time.time()

    return my_deferred

Thanks in advance, I have looked at a lot of examples, but I think I have gotten to the point that I have been looking at this for far too long.

like image 589
Alex Avatar asked May 23 '14 20:05

Alex


People also ask

What is twisted deferred?

twisted. internet. defer. Deferred objects are one of the key concepts that you should understand in order to develop asynchronous code that uses the Twisted framework: they are a signal to the calling function that a result is pending.

What is twisted trial?

Twisted Trial: Asynchronous unit testing framework. Trial extends Python's builtin unittest to provide support for asynchronous tests. Trial strives to be compatible with other Python xUnit testing frameworks.


1 Answers

You have two problems.

Firstly, to get the special deferred handling, your test case needs to inherit from twisted.trial.unittest.TestCase, not the python standard library version.

Lastly, you are not calling reactor.callLater properly, or rather, you aren't giving it the right arguments for Deferred.callback(); you need to give the deferred a value. If you don't care what it is, give it a None. Likewise, the callback needs to take that argument; you can safely ignore it.

from twisted.trial import unittest
#^^^^^^^^^^^^^^^^^
import time

from twisted.internet import defer
from twisted.internet import reactor

class MyTest(unittest.TestCase):

    def test_simple_deferred(self):

        print "\nStarting Test"

        my_deferred = defer.Deferred()

        def print_time(dont_care):
        #              ^^^^^^^^^
            print time.time()
            self.assertTrue(False)

        my_deferred.addCallback(print_time)

        reactor.callLater(3, my_deferred.callback, None)
        #                                        ^^^^^^    
        print time.time()

        return my_deferred
like image 194
SingleNegationElimination Avatar answered Oct 11 '22 16:10

SingleNegationElimination