Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Twisted和inlineCallbacks进行测试_Python_Unit Testing_Testing_Twisted - Fatal编程技术网

Python 使用Twisted和inlineCallbacks进行测试

Python 使用Twisted和inlineCallbacks进行测试,python,unit-testing,testing,twisted,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.T

以下是我的函数定义:

@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和trial的文档,但找不到如何使其工作。

这里是使用@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)
但它确实:

$ 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的实现。

您的函数get_order需要从twisted.internet.defer返回一个Deferred或DeferredList的实例。

谢谢Jean Paul。有趣的我只是忽略了隐藏不必要的实现细节。如果我在服务器视图中运行get_order函数,它将正常工作。我希望它能坏掉,这样就容易修好了。顺便说一句,我使用的是Twisted 10.10。由于使用我的get_order实现可以正常工作,该实现返回defer.succeed3,我认为get_order的执行细节不是不必要的,而是您遇到的问题的核心。谢谢Yavor。我没试过这样的东西,我去看看。但是我更喜欢使用defer.inlineCallbacks和yield。我找不到任何这样的例子,我不确定这是否可能。。
$ 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)
$