使用Twisted和inlineCallbacks进行测试

时间:2012-02-23 04:06:18

标签: python unit-testing testing twisted

这是我的功能定义:

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

我想要做的是使用Twisted的试用版来测试这个功能:

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)

我很困惑..我把所有关于Twisted和试用的文件都删了,但是找不到如何使这个工作。

3 个答案:

答案 0 :(得分:8)

以下是使用@defer.inlineCallbacks和输出的工作测试。 我把你的测试放在最后。它提出了一个例外。

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)

答案 1 :(得分:4)

但确实如此:

$ trial test_example.py 
test_example
  OrderTest
    test_order ...                                                      [ERROR]

===============================================================================
[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 "/tmp/test_example.py", line 11, in test_order
    raise Exception('FAIL FAIL!!') # this should fail, but never does
exceptions.Exception: FAIL FAIL!!

test_example.OrderTest.test_order
-------------------------------------------------------------------------------
Ran 1 tests in 0.009s

FAILED (errors=1)
$

也许真正的问题在于get_order的实现(你已经省略了)。

答案 2 :(得分:0)

您的函数get_order需要从Deferred返回DeferredListtwisted.internet.defer的实例。