Tornado Unittest模拟产量报表

时间:2016-06-28 21:36:25

标签: unit-testing asynchronous mocking tornado tornado-motor

当为龙卷风应用程序运行单元测试时,我不断收到此错误:

tornado.ioloop.TimeoutError: Operation timed out after 5 seconds

这是测试代码:

class TestMongo(testing.AsyncTestCase):

def setUp(self):
    super().setUp()
@patch('stashboard.checkers.events')
@testing.gen_test()
def test_check(self, event_mock,): # tests for Ok connection status
    event_mock.STATUS_OK = events.STATUS_OK
    event_mock.STATUS_FAIL = events.STATUS_FAIL
    event_mock.save.return_value = Future()
    d = {'path': '/test'}
    test = MongoChecker(d, 1, None)
    yield test.check()
    event_mock.save.assert_called_with({'path': '/test'},
                                 {'status': events.STATUS_OK,
                             'address': '#address#'})

这是正在测试的代码:

class MongoChecker(Checker):
# pings Mongo servers noted in configuration to make sure
# they are still running

def __init__(self, event, frequency, params):
    super().__init__(event, frequency, params)
    self.clients = []
    for server in configuration["mongodb"]:
        host = server["host"]
        port = server["port"]
        address = 'mongodb://{}:{}'.format(host, port)
        client = motor.motor_tornado.MotorClient(
            address)
        # creates client to test connection
        client.address = address
        self.clients.append(client)

@gen.coroutine
def check(self):
    for client in self.clients:
        try:
            yield client.admin.command('ping')
            data= {'address': client.address, 'status': events.STATUS_OK}
        except ConnectionError:
            data = {'address': client.address, 'status': events.STATUS_FAIL}
        yield self.save(data)

这是断言实际调用的方法:

@gen.coroutine
def save(self, data):
    yield events.save(self.event, data)

当我从yield self.save(data)中删除yield语句时,测试工作正常。我需要模拟一个Future对象从self.save返回,并从中得到实际结果。

1 个答案:

答案 0 :(得分:1)

您已修补stashboard.checkers.events并将Future设为返回值,因此代码为:

@gen.coroutine
def save(self, data):
    yield events.save(self.event, data)

成为

@gen.coroutine
def save(self, data):
    yield Future()

yield Future表示Tornado将等到给定的未来将由set_resultset_exception解决。在出于安全原因(和理智)的单元测试中,每个测试/套件都有一个时间限制。由于你还没有设置结果,测试会永远持续下去,谢天谢地它的超时。

您可以使用tornado.gen.maybe_future设置协同程序的结果。因此模拟设置可能如下所示:

@patch('stashboard.checkers.events')
@testing.gen_test()
def test_check(self, event_mock,): # tests for Ok connection status
    event_mock.STATUS_OK = events.STATUS_OK
    event_mock.STATUS_FAIL = events.STATUS_FAIL
    event_mock.save.return_value = tornado.gen.maybe_future('some dummy ret')
    # ...