Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing with Twisted and inlineCallbacks

Here is my function definition:

@defer.inlineCallbacks
def get_order(order_id):
    # do some db operations...
    defer.returnValue(order_details)

What I want to do is to test this function using Twisted's trial:

from twisted.trial import unittest
from twisted.internet import defer

class OrderTest(unittest.TestCase):
    @defer.inlineCallbacks  
    def test_order(self):
        order = yield get_order(5)
        raise Exception('FAIL FAIL!!') # this should fail, but never does
        self.assertEqual(order.id, 6)

I'm very confused.. I red all the documents that I could find about Twisted and trial but couldn't find how to make this work.

like image 599
pocoa Avatar asked Mar 06 '26 01:03

pocoa


1 Answers

Here are working tests with @defer.inlineCallbacks and the output. I've put your test in the end. It raises an exception.

from twisted.trial import unittest
from twisted.internet import defer
from twisted.internet import reactor

@defer.inlineCallbacks
def get_order(order_id):   
    d = defer.Deferred()
    reactor.callLater(2, d.callback, order_id)
    result = yield d # yielded deferreds will pause the generator

    # after 2 sec
    defer.returnValue(result) # the result of the deferred, which is order_id

class OrderTest(unittest.TestCase):
    def test_order(self):
        return get_order(6).addCallback(self.assertEqual, 6)

# This works
class OrderTestYourWay(unittest.TestCase):
    @defer.inlineCallbacks
    def test_order(self):
        order_id = yield get_order(6)
        defer.returnValue(self.assertEqual(order_id, 6))

# this fails, cause 6 != 5
class OrderTestYourWayWithFailure(unittest.TestCase):
    @defer.inlineCallbacks
    def test_order(self):
        order_id = yield get_order(6)
        defer.returnValue(self.assertEqual(order_id, 5))

# This is your test
# It raises an exception, which produces an error in the test
class OrderTestRaisingException(unittest.TestCase):
    @defer.inlineCallbacks  
    def test_order(self):
        order_id = yield get_order(5)
        raise Exception('FAIL FAIL!!') # this should fail, but never does
        self.assertEqual(order_id, 6)

yavor@yavor-pc:~/src/tvstream/Misc/src/var$ trial OrderTest.py 

OrderTest

  OrderTest

    test_order ...                                                         [OK]

  OrderTestRaisingException

    test_order ...                                                      [ERROR]

  OrderTestYourWay

    test_order ...                                                         [OK]

  OrderTestYourWayWithFailure

    test_order ...                                                       [FAIL]

===============================================================================

[FAIL]

Traceback (most recent call last):

  File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1020, in _inlineCallbacks

    result = g.send(result)

  File "/home/yavor/src/tvstream/Misc/src/var/OrderTest.py", line 34, in test_order

    defer.returnValue(self.assertEqual(order_id, 5))

twisted.trial.unittest.FailTest: not equal:

a = 6

b = 5


OrderTest.OrderTestYourWayWithFailure.test_order

===============================================================================

[ERROR]

Traceback (most recent call last):

  File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 1020, in _inlineCallbacks

    result = g.send(result)

  File "/home/yavor/src/tvstream/Misc/src/var/OrderTest.py", line 40, in test_order

    raise Exception('FAIL FAIL!!') # this should fail, but never does

exceptions.Exception: FAIL FAIL!!

OrderTest.OrderTestRaisingException.test_order

-------------------------------------------------------------------------------

Ran 4 tests in 8.016s

FAILED (failures=1, errors=1, successes=2)
like image 182
Yavor Atov Avatar answered Mar 08 '26 15:03

Yavor Atov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!