首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用IOLoop测试马达调用

用IOLoop测试马达调用
EN

Stack Overflow用户
提问于 2015-08-11 05:49:47
回答 1查看 580关注 0票数 0

我正在运行马达数据库调用回调中的单元测试,并且在运行nosetest时,我成功地捕获了AssertionErrors并使它们表面显示,但是AssertionErrors被错误地捕捉到了。回溯是针对不同的文件。

我的单元看起来一般如下:

代码语言:javascript
复制
def test_create(self):
    @self.callback
    def create_callback(result, error):
        self.assertIs(error, None)
        self.assertIsNot(result, None)
    question_db.create(QUESTION, create_callback)
    self.wait()

我使用的unittest.TestCase类如下所示:

代码语言:javascript
复制
class MotorTest(unittest.TestCase):
    bucket = Queue.Queue()
    # Ensure IOLoop stops to prevent blocking tests
    def callback(self, func):
        def wrapper(*args, **kwargs):
            try:
                func(*args, **kwargs)
            except Exception as e:
                self.bucket.put(traceback.format_exc())
            IOLoop.current().stop()
        return wrapper

    def wait(self):
        IOLoop.current().start()
        try:
            raise AssertionError(self.bucket.get(block = False))
        except Queue.Empty:
            pass

我所看到的错误:

代码语言:javascript
复制
======================================================================
FAIL: test_sync_user (app.tests.db.test_user_db.UserDBTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/----/Documents/app/app-Server/app/tests/db/test_user_db.py", line 39, in test_sync_user
    self.wait()
  File "/Users/----/Documents/app/app-Server/app/tests/testutils/mongo.py", line 25, in wait
    raise AssertionError(self.bucket.get(block = False))
AssertionError: Traceback (most recent call last):
  File "/Users/----/Documents/app/app-Server/app/tests/testutils/mongo.py", line 16, in wrapper
    func(*args, **kwargs)
  File "/Users/----/Documents/app/app-Server/app/tests/db/test_question_db.py", line 32, in update_callback
    self.assertEqual(result["question"], "updated question?")
TypeError: 'NoneType' object has no attribute '__getitem__'

错误报告在UsersDbTest中,但显然在test_questions_db.py (即QuestionsDbTest)中。

我对nosetest和异步测试有问题,所以如果有人在这方面有任何建议,我们也会非常感激的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-11 13:17:57

如果没有SSCCE,我无法完全理解您的代码,但我要说,您在异步测试方面采取了一种不明智的方法。

您面临的特殊问题是,在离开测试函数之前,不需要等待测试完成(异步),因此在下一次测试中继续循环时,IOLoop中仍有工作挂起。使用自己的“测试”模块--它为启动和停止循环提供了方便的方法,并且它在测试之间重新创建了循环,这样您就不会像报告的那样受到干扰。最后,它有非常方便的手段测试协同。

例如:

代码语言:javascript
复制
import unittest
from tornado.testing import AsyncTestCase, gen_test

import motor

# AsyncTestCase creates a new loop for each test, avoiding interference
# between tests.
class Test(AsyncTestCase):
    def callback(self, result, error):
        # Translate from Motor callbacks' (result, error) convention to the
        # single arg expected by "stop".
        self.stop((result, error))

    def test_with_a_callback(self):
        client = motor.MotorClient()
        collection = client.test.collection
        collection.remove(callback=self.callback)

        # AsyncTestCase starts the loop, runs until "remove" calls "stop".
        self.wait()

        collection.insert({'_id': 123}, callback=self.callback)

        # Arguments passed to self.stop appear as return value of "self.wait".
        _id, error = self.wait()
        self.assertIsNone(error)
        self.assertEqual(123, _id)

        collection.count(callback=self.callback)
        cnt, error = self.wait()
        self.assertIsNone(error)
        self.assertEqual(1, cnt)

    @gen_test
    def test_with_a_coroutine(self):
        client = motor.MotorClient()
        collection = client.test.collection
        yield collection.remove()
        _id = yield collection.insert({'_id': 123})
        self.assertEqual(123, _id)
        cnt = yield collection.count()
        self.assertEqual(1, cnt)

if __name__ == '__main__':
    unittest.main()

(在本例中,我为每个测试创建了一个新的MotorClient,这在测试使用Motor的应用程序时是个好主意。您的实际应用程序不能为每个操作创建一个新的MotorClient。为了获得良好的性能,您必须在应用程序开始时创建 one MotorClient,并在流程的整个生命周期中使用相同的客户端。)

看看测试模块,特别是gen_test装饰器:

http://tornado.readthedocs.org/en/latest/testing.html

这些测试设备负责处理与单元测试龙卷风应用程序相关的许多细节。

我做了一个演讲,写了一篇关于龙卷风测试的文章,这里有更多的信息:

http://emptysqua.re/blog/eventually-correct-links/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31934097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档