使用 Twisted 和 inlineCallbacks 进行测试 [英] Testing with Twisted and inlineCallbacks

查看:24
本文介绍了使用 Twisted 和 inlineCallbacks 进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的函数定义:

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

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

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)

我很困惑..我把我能找到的关于 Twisted 和 Trial 的所有文档都涂红了,但找不到如何使它起作用.

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.

推荐答案

以下是使用 @defer.inlineCallbacks 和输出进行的工作测试.我已经把你的测试放在最后.它引发了一个异常.

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)

这篇关于使用 Twisted 和 inlineCallbacks 进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆