2015-10-07 32 views
1

我使用Rethinkdb和旋風單元測試與rethinkdb.set_loop_type("tornado")拆機不叫與旋風

我使用python的單元測試來測試我的服務器路線。

這裏是我的單元測試的基類:

class ServerTest(AsyncHTTPTestCase): 
    def setUp(self): 
     super(ServerTest, self).setUp() 

    def get_app(self): 
     return Application(self.routes, debug = False) 

    def post(self, route, data): 
     result = self.fetch("/%s" % route, method = "POST", 
           body = json.dumps(data)).body 
     return json.loads(result) 

    def tearDown(self): 
     super(ServerTest, self).tearDown() 
     conn = yield r.connect() 
     yield r.db("test").table("test_table").delete().run(conn) 
     conn.close() 

我注意到,setUp運行正常,但tearDown不是。我所有的單元測試都正確傳遞,但是不會調用tearDown中的打印語句。

編輯:我縮小了它的事實,我打電話yieldD撕下。

編輯:添加@ gen.coroutine到拆解顯示打印語句,但不執行數據庫

回答

3

使用yield@gen.coroutine上刪除使函數異步,從而改變它的接口:主叫請注意這一變化。 unittest框架不知道協程的任何內容,因此unittest所稱的方法可能不是協程。

相反的@gen.coroutine,您可以使用@tornado.testing.gen_test,它可以讓你在測試中使用yield,並從setUptearDown調用的方法,而不是setUptearDown自己(因爲發電機機械無法super().setUp()之前或super().tearDown()後工作。使用與gen_test一個輔助方法和tearDown稱之爲沒有yield

def tearDown(self): 
    self.tearDownHelper() 
    super(ServerTest, self).tearDown() 

@tornado.testing.gen_test 
def tearDownHelper(self): 
    conn = yield r.connect() 
    yield r.db("test").table("test_table").delete().run(conn) 
    conn.close()