我通常会模拟输入并仅测试输出。这是一个使用这个模拟库的人为示例 - http://www.voidspace.org.uk/python/mock/。您必须模拟出正确的 mongodb 查询函数。我不确定你在用什么。
from mock import Mock, patch
import json
@patch('my_tornado_server.mongo_db_connection.query')
def test_a_random_handler_returns_some_json(self, mock_mongo_query):
request = Mock()
# Set any other attributes on the request that you need
mock_mongo_query.return_value = ['pink', 'orange', 'purple']
application = Mock()
handler = RandomHandler(application, request)
handler.write = Mock()
handler.get('some_arg')
self.assertEqual(handler.write.call_args_list, json.dumps({'some': 'data'}))